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

I made a BroadcastReceiver; put in manifest this code:

  • How to create lines in xml
  • android duplicate provider authority
  • Is it possible to get the mac address of client devices which are within wifi hotspot range?
  • How to get from a MySql server to an Android app?
  • Android background downloads statistic
  • Get Current Fragment and Save on onSaveInstanceState() Method For Screen Orientation
  • <receiver android:name="MyIntentReceiver">
                    <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" />    

    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;
        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);

    And the result is – both ACTION_SCREEN_ON
    and ACTION_SCREEN_OFF never fired!
    USER_PRESENT and BOOT_COMPLETED worked fine but the other did not. I’m using an emulator, not a real device – can this cause the problem?

    Any help?
    I need to catch the screen on and off in order to enable/disable WiFi
    to save battery.

    Thanks in advance

    Related posts:

    Android saving Bitmap to SD card
    Change Application Package name through Android Studio
    How to look up user information from Google GAIA ID?
    &ldquo;No, missing feature: WATCH&rdquo; when I try to run my smartphone app with wear app?
    java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/animation/AnimatorCompatHe...
    Dialog box with EditText and Spinner
  • Android memory leak in Apache Harmony's JarURLConnectionImpl?
  • disable rating on RatingBar
  • Dagger 2, sometimes on compiling I get “cannot find symbol class DaggerApplicationComponent”
  • is it possible to see application data from adb shell the same way I see it mounting SD card?
  • Android two AsyncTasks serially or parallel execution? - The second is freezing but the result is ok
  • How to invoke the ActionBar's ContextMenu-like behavior?
  • 2 Solutions collect form web for “ACTION_SCREEN_ON and ACTION_SCREEN_OFF not working?”

    You cannot catch those intents through XML (I forget why). However, you could use a Service that registers a BroadcastReceiver member in its onStartCommand() and unregisters it in its onDestroy(). This would require the service to be running in the background, constantly or as long as you need it to, so be sure to explore alternative routes.

    You could define the BroadcastReceiver in your Service class like so:

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

    For a slightly complicated example, but one that shows how the BroadcastReceiver and Service interact see CheckForScreenBugAccelerometerService from my app, ElectricSleep.

    To capture the SCREEN_OFF and SCREEN_ON actions (and maybe others) you have to configure the BroadcastReceiver by code, not through the manifest.

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

    It’s tested and works right.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.