如何显示屏幕已锁定?
在我的应用程序中,我需要知道设备何时被锁定(在 HTC 上,它看起来像是短按“电源”按钮)。那么问题是:设备锁定时会触发哪个事件?或者设备将要休眠?
In my application I need to know when device is locked (on HTC's it looks like short press on "power" button). So the question is: which event is triggered when device is locked? Or device is going to sleep?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该扩展
BroadcastReceiver
并实现onReceive
,如下所示:然后您只需注册它,当屏幕关闭时您将开始接收事件:
You should extend
BroadcastReceiver
and implementonReceive
, like this:Then you just have to register it and you'll start receiving events when the screen is switched off:
有一个更好的方法:
There is a better way:
除了上述答案之外,如果您想在应用程序位于前台时触发某些操作:
您可以使用名为onResume()的事件来触发您的应用程序当您的应用程序从之前的静止状态中获得关注时,即,如果您的应用程序位于后台(暂停/最小化...)
In addition to the above answer, in-case you want to trigger some action when your app is at the foreground:
You could use the event called onResume() to trigger your own function when your app takes the spotlight from a previously resting state, i.e, if your app was at the background(paused/minimized...)