Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

android - ACTION_SCREEN_ON and ACTION_SCREEN_OFF not working?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...