tags:

views:

98

answers:

3

As mentioned in a previous question, I am having difficulty intercepting all android.intent.action.SCREEN_ON events 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.

+1  A: 

Can the system broadcast of the ACTION_USER_PRESENT intent be of any help?

qtips
+1  A: 

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.

CommonsWare
+1  A: 

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.

hackbod
Actually it possible to receive the broadcasts, as BroadcastReceivers on a long-lived Service. My app does this quite happily, but I'm trying to nix the long-lived Service. It just fails silently if you supply it as a intent-filter on a receiver.
Jim Blackler