views:

641

answers:

2

I want to figure out how to detect when the phone wakes up from being in the black screen mode and write a handler for that event. Is that possible? It seems like this would be something a Broadcast Receiver should handle? Or is there a better or more proper way?

Thanks

+1  A: 

If you have a Service that it active you can catch these events with

registerReceiver(new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
    // do something
  }
}, new IntentFilter(Intent.ACTION_SCREEN_ON));

However this relies on having a perpetually running service which I have recently learned is discouraged because it is brittle (the OS likes to close them) and uses resources permanently.

Disappointingly, it seems it is not possible to have a receiver in your manifest that intercepts SCREEN_ON events.

This has come up very recently:

http://stackoverflow.com/questions/2575242/android-intent-action-screen-on-doesnt-work-as-a-receiver-intent-filter

also

http://stackoverflow.com/questions/1588061/android-how-to-receive-broadcast-intents-action-screen-on-off

Jim Blackler
ah thanks so that will detect the screen on and allow me to properly handle it right?I was reading your thread and you don't recommend having a background service running and waiting for the phone to wake up. Why? What's the downside and is there any way around? Isn't running background services how you detect events like this?
Joe
This article discusses the background service issue - http://www.androidguys.com/2010/03/29/code-pollution-background-control/
Jim Blackler
A: 

You are right on the broadcast receiver. You could listen to the SCREEN_ON and SCREEN_OFF broadcast events.

Soulreaper