ACTION_SCREEN_ON and ACTION_SCREEN_OFF not working?

I am trying to turn off WiFi when the screen is off (locked), and turn it on again when the screen is on (unlocked).

I made BroadcastReceiver ; enter this code:

 <receiver android:name="MyIntentReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.SCREEN_OFF" /> <action android:name="android.intent.action.SCREEN_ON" /> <action android:name="android.intent.action.USER_PRESENT" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </receiver> 

and this is the class MyIntentReceiver :

 package org.androidpeople.boot; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyIntentReceiver extends BroadcastReceiver { // Called when boot completes public static boolean startup; @Override public void onReceive(Context context, Intent intent) { // Set what activity should launch after boot completes System.out.println("Intent Action: " + intent.getAction()); if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { System.out.println("locked : ACTION_SCREEN_OFF"); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { System.out.println("not locked : ACTION_SCREEN_ON "); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { System.out.println("User Unlocking it "); } else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { // this to indicate that program is running // automaticlly not manually by user startup = true; System.out.println("Automatic BOOT at StartUp"); Intent startupBootIntent = new Intent(context, LaunchActivity.class); startupBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(startupBootIntent); } } } 

And the result is that both ACTION_SCREEN_ON and ACTION_SCREEN_OFF never fired! USER_PRESENT and BOOT_COMPLETED worked fine, while the other BOOT_COMPLETED not. I use an emulator, not a real device - can this cause a problem?

Any help? I need to turn on and off the screen to enable / disable WiFi to save battery power.

Thanks in advance

+7
source share
2 answers

You cannot catch these intentions through XML (I forget why). However, you can use a Service that registers a BroadcastReceiver member in its onStartCommand() and cancels it in its onDestroy() . This will require the service to run in the background, constantly or as long as you need it, so be sure to explore alternative routes.

You can define BroadcastReceiver in your Service class as follows:

 private final class ScreenReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { //stuff } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { //other stuff } } } 

For a slightly complicated example, but which shows how BroadcastReceiver and Service interact, see CheckForScreenBugAccelerometerService from my application, ElectricSleep.

+8
source

In order to capture the actions of SCREEN_OFF and SCREEN_ON (and possibly others), you must configure BroadcastReceiver by code, and not through the manifest.

 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); intentFilter.addAction(Intent.ACTION_SCREEN_OFF); BroadcastReceiver mReceiver = new ScreenStateBroadcastReceiver(); registerReceiver(mReceiver, intentFilter); 

It has been tested and works correctly.

+12
source

All Articles