即使应用程序处于休眠状态也要检测屏幕锁

问题描述 投票:0回答:1

我有一个Android应用程序,每当用户按下锁定按钮时,都需要将其锁定(重定向到登录页面)。用户可以在我的应用程序或其他应用程序/主屏幕上锁定手机。在这两种情况下,我都需要锁定我的应用程序。

如果手机从应用程序锁定,这很好用:

@Override
protected void onStop() {
    super.onStop();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    boolean isScreenOn = pm.isInteractive();
    if (!isScreenOn) {
        LogUtils.logD(TAG, "Screen is off, Locking the application");
        // Lock the application code
    }
}

但是当用户离开应用程序然后锁定它时,我无法确定第二种情况。我不想为此目的启动服务或任何后台线程。

android wakelock
1个回答
0
投票

尝试添加其他代码,然后在代码末尾返回,以便活动仍在后台进程中唤醒

@Override
protected void onStop() {
super.onStop();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();
if (!isScreenOn) {
    LogUtils.logD(TAG, "Screen is off, Locking the application");
    // Lock the application code
} else{
  return true;
}
© www.soinside.com 2019 - 2024. All rights reserved.