Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write an Android broadcast receiver that detects when the phone wakes up?

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?

like image 967
Joe Avatar asked Apr 05 '10 07:04

Joe


People also ask

Can we create custom broadcast receiver in Android?

APPROACH : Create your own receiver class which will extend the BroadcastReceiver class of the default android. content package. You will need to override the onRecieve() method, which will take context and intent as params.

Does broadcast receiver work when app is killed?

Please note, that when an app is killed via "FORCE CLOSE" by the user, all the app's broadcast receiver are immediately put on pause, and the system prevents them from receiving future broadcasts until the user manually reopens that app.

What is BroadcastReceiver Android?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

What is the time limit of broadcast receiver in Android?

What is the time limit of broadcast receiver in Android? There is also a 5-10 second limit, after which Android will basically crash your app. However, you cannot reliably fork a background thread from onReceive() , as once onReceive() returns, your process might be terminated, if you are not in the foreground.


2 Answers

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:

android.intent.action.SCREEN_ON doesn't work as a receiver intent filter

also

Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?

like image 112
Jim Blackler Avatar answered Sep 28 '22 03:09

Jim Blackler


You could also have a broadcast receiver that catches the USER_PRESENT broadcast intent for when the user has unlocked the device. Naturally some versions of Honeycomb don't honor this but for all non brain-dead versions of Android (2.x and 4.x), it works nicely.

like image 38
MattC Avatar answered Sep 28 '22 03:09

MattC