解锁设备,显示文本,然后再次锁定



为了我的应用程序的需要,即使启用了锁屏,我也需要在屏幕上显示一条消息,然后等待 3 秒钟,而不是我必须再次锁定手机,因为我不希望它在您的口袋里拨打不需要的电话。

第一部分很简单:

if (PreferenceManager.getDefaultSharedPreferences(
    getBaseContext()).getBoolean("wake", false)) {
    KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
    WakeLocker.acquire(ProtoBenService.this);
    Intent myIntent = new Intent(ProtoBenService.this,LockActivity.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (isKeyguardUp) {
        ProtoBenService.this.startActivity(myIntent);
    } else
    Toast.makeText(ProtoBenService.this.getBaseContext(), intention, Toast.LENGTH_LONG).show();
    WakeLocker.release();
}

使用此类:

public abstract class WakeLocker {
    private static PowerManager.WakeLock wakeLock;
    public static void acquire(Context ctx) {
        if (wakeLock != null) wakeLock.release();
        PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "CobeIm");
        wakeLock.acquire();
    }
    public static void release() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }
}

和活动:

public class LockActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        TextView tv = new TextView(this);
        tv.setText("This is working!");
        tv.setTextSize(45);
        setContentView(tv);
        Runnable mRunnable;
        Handler mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {
                LockActivity.this.finish();
            }
        };
        mHandler.postDelayed(mRunnable, 3 * 1000);
    }
}

所以,这很好,手机可以显示我的文字!

唯一的问题是当我想再次锁定手机时,似乎锁定手机受系统保护......

  • 以编程方式关闭屏幕并锁定手机
  • 如何以编程方式锁定安卓

我认为我的用户不会理解设备管理员,也无法激活它。有什么解决方法可以在没有设备管理员的情况下锁定屏幕吗?

我使用以下方法来锁定和解锁手机。

初始 化

        KeyguardLock keyguard;
        KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        keyguard = km.newKeyguardLock("MyApp");

解锁手机

keyguard.disableKeyguard();

再次锁定手机

keyguard.reenableKeyguard();    

打开屏幕

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

并且不要忘记使用以下权限

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

我很确定您必须使用设备管理功能来锁定屏幕。

    protected static void initiateDeviceLock(Context context) {
    ComponentName componentName = new ComponentName(context, MyDeviceAdminReceiver.class);
    DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
    boolean active = dpm.isAdminActive(componentName);
    Log.i(context.getClass().getSimpleName(), "Active (in initiateDeviceLock) = " + String.valueOf(active));
    if (active) {
        dpm.lockNow();
    }
}

为了帮助用户设置设备管理员功能,您可以将他们带到设置页面:

    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    ComponentName componentName = new ComponentName(TestActivity.this, MyDeviceAdminReceiver.class);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
    startActivityForResult(intent, CODE);

相关内容

  • 没有找到相关文章

最新更新