检测屏幕何时锁定

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

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

我有一个播放音乐的游戏。当按下锁定按钮时,它会继续播放。我原本让音乐在

OnStop
停止,但应用程序在锁定后会重新启动,所以音乐最终会再次启动。

然后,我将

KeyboardHidden|orientation
添加到清单中。这使得它不会重新启动应用程序,但
OnStop
似乎不再被调用。

我尝试使用

PowerManager
来查看屏幕是否打开/关闭,这可以工作,但没有帮助。 (我可以让音乐停在那里,但是一旦你再次按下锁定按钮,音乐就会重新开始)

android screen
9个回答
112
投票

有更好的方法:

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

不需要广播接收器、权限或类似的东西。

注意:当用户将屏幕锁定设置为无时,此功能不起作用 在设置-->安全-->屏幕锁定-->无


32
投票

此链接可能对其他人在一个地方做两件事有帮助。

检查设备是否已锁定:

KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
boolean isPhoneLocked = myKM.inKeyguardRestrictedInputMode();

(注意:如果

screenlock
中的
none
设置为
settings-->security-->screenlock
,则上述方法不起作用。)

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

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

17
投票

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

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);
}

之后这会给我类似的输出

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

14
投票

取自此链接: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;
}

5
投票

您可以使用

KeyguardManager
类来检查屏幕是否锁定。下面是用 Kotlin 编写的代码片段。

val keyguardManager: KeyguardManager = context?.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager

if (keyguardManager.isKeyguardLocked) {
   // device locked logic goes here 
} else {
   // device unlocked case
}

3
投票

下面的代码显示屏幕是否锁定。

 private void checkPhoneScreenLocked(){
        KeyguardManager myKM = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
        if( myKM.isKeyguardLocked()) {
            System.out.println("Phone is locked");
        } else {
            System.out.println("Phone is not locked");
        }
    }

缺点:-只有当用户选择了除无之外的任何安全模式时,我们才能检查手机是否被锁定。


2
投票

我建议这样:

private void registerBroadcastReceiver() {
    final IntentFilter theFilter = new IntentFilter();
    /** System Defined Broadcast */
    theFilter.addAction(Intent.ACTION_SCREEN_ON);
    theFilter.addAction(Intent.ACTION_SCREEN_OFF);

    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_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);
}

@Shaul Rosenzweig 的上述答案与其他可用于检测屏幕打开和关闭状态的帖子相结合。我在 Samsung Galaxy S4 Mini 上测试了这个解决方案,效果很好。


0
投票

Luv Kumar 的答案有效,但只有当用户明确锁定屏幕(通过按下锁定按钮)时才会注册。除此之外,我希望我的应用程序能够告诉屏幕何时关闭(例如屏幕超时),就是这样做的:

刚刚向 myKM.inKeyguardRestrictedInputMode() 添加了另一个选项

if( myKM.inKeyguardRestrictedInputMode() || (strAction.equals(Intent.ACTION_SCREEN_OFF)) 

0
投票

使用 KeyguardManager.isDeviceLocked 检测设备当前是否已锁定并需要 PIN、图案或密码才能解锁。

参考:https://developer.android.com/reference/android/app/KeyguardManager#isDeviceLocked()

fun isDeviceLocked(context: Context): Boolean {
    return (context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager).isDeviceLocked
}
© www.soinside.com 2019 - 2024. All rights reserved.