开发者

Detect when an application is keeping the screen on

开发者 https://www.devze.com 2023-04-12 19:09 出处:网络
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

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,


I think the best you can get is PowerManager.isScreenOn();


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号