检测应用程序何时保持屏幕打开
我正在编写一个 Android 应用程序,主要通过状态栏通知与用户进行通信。但是,如果用户没有真正直接与手机交互,我希望不打扰用户。例如,如果用户正在使用导航应用程序或观看电影。我认为所有(或大多数)此类应用程序的共同点是它们使屏幕保持打开状态。我相信应用程序可以通过使用 WakeLock 或在其活动上指定 keepScreenOn 参数来保持屏幕打开,可能还有其他方法。
我想知道在我要显示通知之前,我是否可以检查另一个应用程序是否保持屏幕打开,这样我就可以避免打扰用户。我想涵盖所有选项。我可以看到 WakeLock 有一个 isHeld() 方法,但是它会检测其他方法吗?
谢谢你,
I'm writing an Android app that mostly communicates with the user via status bar notifications. However, I would like to not bother the user if s/he is not really directly interacting with their phone. For example, if the user is using the navigation app or watching a movie. I think that the commonality between all (or most) of these kind of apps is that they keep the screen on. I believe an app can keep the screen on by using a WakeLock or by specifying a keepScreenOn parameter on their activity, there might be other ways.
I wanted to know if, before I am about to show a notification, I can check if another application is keeping the screen on so I can avoid bothering the user. I'd like to cover all options. I can see that WakeLock has an isHeld() method, but will that detect the other method?
Thank you,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你能得到的最好的是 PowerManager.isScreenOn() ;
I think the best you can get is PowerManager.isScreenOn();
使用 ACQUIRE_CAUSES_WAKEUP 标志获取屏幕明亮唤醒锁。您必须获得此唤醒锁,因为如果没有,它将强制屏幕打开。即使屏幕已经打开,也不能保证它会保持打开状态直到您的应用程序运行,除非您获得唤醒锁。完成后不要忘记释放唤醒锁。
Acquire screen bright wakelock with ACQUIRE_CAUSES_WAKEUP flag. U have to acquire this wakelock bcoz it will force the screen to turn ON if its not. Even if the screen is already ON, one can not guarantee it will remain ON till ur application is running unless u acquire the wakelock. Dont forget to release the wakelock when u r done.
我认为无论状态如何,您都可以在状态栏上发布通知 - 需要用户充分关注的应用程序通常会完全禁用状态栏的显示 -
因此您的更新不会造成任何损害。
I think you can just post notifications on status bar regardless of state - applications requiring full user attention will typically disable display of status bar altogether -
so no harm will be done by your updates.