How to detect when the screen is on?
As mentioned in a previous question, I am having difficulty intercepting all android.intent.action.SCREEN_ON
eve开发者_如何学Gonts without a long-lived Service
(discouraged).
I may be able to work around the need if I can simply work out when the screen is on at any given time, in the Service
.
Can anyone suggesting a method call that would return this information? 1.5 upwards.
PowerManager.isScreenOn()
Since: API Level 7
Can the system broadcast of the ACTION_USER_PRESENT intent be of any help?
If your code is running, there's a very good chance the screen is on. The main exception would be if your code is woken up via AlarmManager
.
It would be nice if ACTION_SCREEN_ON
were a sticky broadcast, but that doesn't seem to be the case.
If you can roll with Android 2.1 and newer, PowerManager
now has an isScreenOn()
method. The good news is, that's available. The bad news is, since it was added there, it is unlikely that there is any other way to readily determine it. And, a quick inspection of the source code doesn't give much hope that you might hack your way to the information in earlier SDKs.
The system deliberately doesn't allow applications to receive ACTION_SCREEN_ON
and ACTION_SCREEN_OFF
broadcasts in their manifest, because we very much do not want to have to launch a bunch of apps to turn the screen on or off. This is something that happens all of the time, and must be as fast as possible.
Unfortunately, as pointed out, there isn't a way to check immediately to see if the screen is on.
精彩评论