android 判断锁屏,android-检测何时锁定屏幕

android-检测何时锁定屏幕

请原谅我,这让我发疯了,我会尽力发烧,使自己发疯。

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

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

然后,我在清单中添加了KeyboardHidden | orientation。 这样一来,它就不会重新启动应用程序,但是似乎不再调用OnStop。

我试过使用PowerManager来查看屏幕是否打开/关闭,这是可行的,但没有帮助。 (我可以让音乐停止在那儿,但是一旦您再次按下锁定按钮,音乐就会立即开始备份)

Ryan asked 2020-01-15T02:41:05Z

6个解决方案

96 votes

有一个更好的方法:

KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);

if( myKM.inKeyguardRestrictedInputMode()) {

//it is locked

} else {

//it is not locked

}

不需要广播接收,权限或任何类似的东西。

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

Shaul Rosenzweig answered 2020-01-15T02:41:36Z

28 votes

此链接可能对其他人在一处的两件事有帮助。

检查设备是否被锁定:

KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);

boolean isPhoneLocked = myKM.inKeyguardRestrictedInputMode();

(注意:如果在settings-->security-->screenlock中将screenlock设置为none,则上述方法无效。)

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

PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);

isScreenAwake = (Build.VERSION.SDK_INT < 20? powerManager.isScreenOn():powerManager.isInteractive());

Sagar Shah answered 2020-01-15T02:42:09Z

10 votes

上面的解决方案是正确的,但是当我获得输出时,它给了我一个在屏幕关闭时锁定的输出,而在屏幕打开时锁定了输出,但是当我放置解锁图案后解锁设备时却没有给出解锁输出。所以这是我的解决方案。

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

Love answered 2020-01-15T02:42:34Z

8 votes

从此链接中获取:[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;

}

Alécio Carvalho answered 2020-01-15T02:42:54Z

2 votes

我建议这样做:

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的上述答案与其他可用于检测屏幕打开和关闭状态的帖子结合在一起。 我在三星Galaxy S4 Mini上测试了该解决方案,对我来说效果很好。

class Android answered 2020-01-15T02:43:18Z

0 votes

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

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

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

danjar answered 2020-01-15T02:43:43Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值