Android - How to Receive Broadcast Intents Action_Screen_On/Off

Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?

I believe that those actions can only be received by receivers registered in Java code (via registerReceiver()) rather than through receivers registered in the manifest.

Android how to receive Broadcast when Screen is ON

My code works. Just my phone unstable, I reformat my phone then my app works perfectly. Thanks for devote.

phone:Xiaomi Mi 5
OS:Android 6.0

Getting SCREEN_ON and SCREEN_OFF intents from a widget

My solution is to start a service in the public void onReceive(Context context, Intent intent) method in the AppwidgetProvider subclass. Like:

        if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_ENABLED)) {
Intent listenerService=new Intent(context,ScreenMoniterService.class);
startService(listenerService);
return;
}

Then in the public void onCreate() method of this service, register the BroadcastReceiver and in the public void onDestroy() method, unregister it.
Of course, you should stop that service when all of the appwidget are deleted.

        if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_DISABLED)) {

Intent listenerService=new Intent(context,ScreenMoniterService.class);
stopService(listenerService);
return;
}

Listening for ACTION_SCREEN_OFF

You cannot declare ACTION_SCREEN_ON and ACTION_SCREEN_OFF in the AndroidManifest.xml.
You are only allowed to catch them while your activity is running.

Here's an example.

The BroadcastReceiver:

public class ScreenReceiver extends BroadcastReceiver {

public static boolean wasScreenOn = true;

@Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
wasScreenOn = true;
}
}

}

The Activity:

public class ExampleActivity extends Activity {

private BroadcastReceiver mReceiver = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// initialize receiver
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
// your code
}

@Override
protected void onPause() {
// when the screen is about to turn off
if (ScreenReceiver.wasScreenOn) {
// this is the case when onPause() is called by the system due to a screen state change
Log.e("MYAPP", "SCREEN TURNED OFF");
} else {
// this is when onPause() is called when the screen state has not changed
}
super.onPause();
}

@Override
protected void onResume() {
super.onResume();
// only when screen turns on
if (!ScreenReceiver.wasScreenOn) {
// this is when onResume() is called due to a screen state change
Log.e("MYAPP", "SCREEN TURNED ON");
} else {
// this is when onResume() is called when the screen state has not changed
}
}

@Override
protected void onDestroy() {
if (mReceiver != null) {
unregisterReceiver(mReceiver);
mReceiver = null;
}
super.onDestroy();
}

}

You could probably solve your question by listening to these events from a Service.

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


Following sage advice from CommonsWare
I have elected to try to remove the
long-living Service and use different
techniques.

Actually, I believe my advice was more of a light blue... :-)

But I still need to detect the screen
off and on events.

There are certain events that Android does not want to start up new processes for, so the device does not get too slow from all sorts of stuff all having to run at once. ACTION_SCREEN_ON is one of those. See this previous question for light blue advice on that topic.

So, you need to ask yourself, "Self, do I really need to get control on those events?". The core Android team would like it if your answer was "no".

Android broadcast receiver doesn't receive ACTION_SCREEN_ON

It's work for me on Android 4.0.4

BroadcastReceiver receiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
if (intent == null)
return;
//do something you need when broadcast received

}
};
IntentFilter filter = new IntentFilter()
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);

context.registerReceiver(receiver, filter);

As for register receiver with action Intent.ACTION_SCREEN_ON and Intent.ACTION_SCREEN_OFF in Manifest.xml it don't help because Android code in PowerManagerService.java following:

...
mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
...

android: broadcast receiver for screen on and screen off

The two actions for screen on and off are:

android.intent.action.SCREEN_OFF
android.intent.action.SCREEN_ON

But if you register a receiver for these broadcasts in a manifest, then the receiver will not receive these broadcasts.

For this problem, you have to create a long running service, which is registering a local broadcast receiver for these intents. If you do this way, then your app will look for screen off only when your service is running which won't irritate user.

PS: start the service in foreground to make it running longer.

A simple code snippet will be something like this:

IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(mScreenStateReceiver, screenStateFilter);

Don't forget to unregister the receiver in the Service's onDestroy:

unregisterReceiver(mScreenStateReceiver);

Just in case for people who are asking why the receiver does not work with the declare broadcasts in manifest for ACTION_SCREEN_ON and ACTION_SCREEN_OFF:

https://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_ON https://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF

You cannot receive this through components declared in manifests, only
by explicitly registering for it with Context.registerReceiver().

This is a protected intent that can only be sent by the system.



Related Topics



Leave a reply



Submit