检测屏幕何时锁定 [英] Detecting when screen is locked

查看:99
本文介绍了检测屏幕何时锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've seen a couple of posts on here on how to check if the screen is locked, but none of it has been working for me. It all detects if the actual screen is off or not (not if it's locked).

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

I have a game in which music plays. When the lock button is pressed, it continues to play. I originally had the music stopping in OnStop, but the application would restart after getting locked, so the music would eventually start up again.

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

Then, I added KeyboardHidden|orientation to the manifest. This makes it so it doesn't restart the app, but OnStop doesn't seem to get called anymore.

我尝试使用PowerManager来查看屏幕是否处于打开/关闭状态,该方法可以正常工作,但无济于事. (我可以将音乐停在那里,但是只要您再次按一下锁定按钮,音乐就会立即开始备份)

I've tried using PowerManager to see if the screen is on/off, which works, but doesn't help. (I can get the music to stop there, but as soon as you hit the lock button again, the music starts right back up)

推荐答案

还有一种更好的方法:

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

无需广播接收,权限或任何类似内容.

No need for broadcastRecievers, permissions or anything similar.

注意:当用户将屏幕锁定设置为无"时,该按钮将不起作用 在设置中->安全性->屏幕锁->无

Note: It doesn't work when user has set his/her screen lock to none in settings-->security-->screenlock-->none

这篇关于检测屏幕何时锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆