检测屏幕何时锁定

Rya*_*yan 54 android screen

原谅我,这让我疯了,我会尝试通过我的愤怒发布一些易读的东西.

我在这里看到了几个关于如何检查屏幕是否被锁定的帖子,但没有一个对我有效.它都会检测实际屏幕是否关闭(如果它已锁定).

我有一个音乐播放的游戏.按下锁定按钮后,它将继续播放.我最初在OnStop上停止了音乐,但应用程序会在锁定后重新启动,因此音乐最终会再次启动.

然后,我在清单中添加了KeyboardHidden | orientation.这使得它不会重新启动应用程序,但OnStop似乎不再被调用.

我已经尝试使用PowerManager查看屏幕是否打开/关闭,这有效,但没有帮助.(我可以让音乐停在那里,但只要再次按下锁定按钮,音乐就会立即启动)

Sha*_*eig 96

有一个更好的方法:

KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
 //it is locked
} else {
 //it is not locked
}
Run Code Online (Sandbox Code Playgroud)

无需广播阅读器,权限或任何类似的东西.

注意:当用户在设置 - > security - > screenlock - > none中将其屏幕锁定设置为none时,它不起作用

  • `inKeyguardRestrictedInputMode()` 方法在 API 级别 28 中已弃用。请改用 `isKeyguardLocked()`,该方法自 API 级别 16 起可用。 (5认同)
  • 您如何持续聆听?在计时器里? (2认同)

Sag*_*hah 28

在一个地方,这个链接可能对其他人有用.

检查设备是否锁定:

KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
boolean isPhoneLocked = myKM.inKeyguardRestrictedInputMode();
Run Code Online (Sandbox Code Playgroud)

(注意:如果screenlock设置为nonein,则上述方法不起作用settings-->security-->screenlock.)

检查设备是否处于唤醒状态或处于睡眠模式:(对于Sdk版本> L预览<Sdk版本)

PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
isScreenAwake = (Build.VERSION.SDK_INT < 20? powerManager.isScreenOn():powerManager.isInteractive());
Run Code Online (Sandbox Code Playgroud)

  • @FiReTiTi,这里http://stackoverflow.com/questions/3572463/what-is-context-on-android ... cmon man .... (2认同)

小智 10

上面的解决方案是正确的,但当我得到输出时,它给我一个输出锁定屏幕关闭,并在屏幕打开时锁定,但是当我解锁设备解锁设备后没有给出解锁输出.所以这是我的解决方案.

private void registerBroadcastReceiver() {

     final IntentFilter theFilter = new IntentFilter();
    /** System Defined Broadcast */
    theFilter.addAction(Intent.ACTION_SCREEN_ON);
    theFilter.addAction(Intent.ACTION_SCREEN_OFF);
    theFilter.addAction(Intent.ACTION_USER_PRESENT);

    BroadcastReceiver screenOnOffReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String strAction = intent.getAction();

            KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if(strAction.equals(Intent.ACTION_USER_PRESENT) || strAction.equals(Intent.ACTION_SCREEN_OFF) || strAction.equals(Intent.ACTION_SCREEN_ON)  )
                if( myKM.inKeyguardRestrictedInputMode())
                {
                    System.out.println("Screen off " + "LOCKED");
                } else
                {
                    System.out.println("Screen off " + "UNLOCKED");
                }

        }
    };

    getApplicationContext().registerReceiver(screenOnOffReceiver, theFilter);
}
Run Code Online (Sandbox Code Playgroud)

之后,这会给我输出像

I/System.out:LOCKED 
when i off the mobile screen
I/System.out:LOCKED
when i on the mobile screen
I/System.out:UNLOCKED
when i unlock the mobile after pattern lock
Run Code Online (Sandbox Code Playgroud)


Alé*_*lho 8

从此链接获取:https : //gist.github.com/Jeevuz/4ec01688083670b1f3f92af64e44c112

/**
 * Returns true if the device is locked or screen turned off (in case password not set)
 */
public static boolean isDeviceLocked(Context context) {
    boolean isLocked = false;

    // First we check the locked state
    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    boolean inKeyguardRestrictedInputMode = keyguardManager.inKeyguardRestrictedInputMode();

    if (inKeyguardRestrictedInputMode) {
        isLocked = true;

    } else {
        // If password is not set in the settings, the inKeyguardRestrictedInputMode() returns false,
        // so we need to check if screen on for this case

        PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            isLocked = !powerManager.isInteractive();
        } else {
            //noinspection deprecation
            isLocked = !powerManager.isScreenOn();
        }
    }

    Loggi.d(String.format("Now device is %s.", isLocked ? "locked" : "unlocked"));
    return isLocked;
}
Run Code Online (Sandbox Code Playgroud)