ACTION_SCREEN_ON and ACTION_SCREEN_OFF not working

2020-02-09 04:46发布

问题:

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:

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

回答1:

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 {

    @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 one that shows how the BroadcastReceiver and Service interact see CheckForScreenBugAccelerometerService from my app, ElectricSleep.



回答2:

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);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenStateBroadcastReceiver();
registerReceiver(mReceiver, intentFilter);

It's tested and works right.