ACTION_SCREEN_ON和ACTION_SCREEN_OFF不起作用?

时间:2011-09-09 19:39:08

标签: android android-emulator android-manifest

我正在尝试在屏幕关闭(锁定)时关闭WiFi,并在屏幕开启(解锁)时再次打开。

我做了BroadcastReceiver;放入清单这段代码:

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

这是班级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);
        }

    }
}

结果是 - ACTION_SCREEN_ON 并且ACTION_SCREEN_OFF从未被解雇! USER_PRESENTBOOT_COMPLETED工作正常,但另一方则没有。我正在使用模拟器,而不是真正的设备 - 这会导致问题吗?

有任何帮助吗? 我需要打开和关闭屏幕才能启用/禁用WiFi 节省电池。

提前致谢

2 个答案:

答案 0 :(得分:13)

要捕获SCREEN_OFF和SCREEN_ON操作(可能还有其他操作),您必须按代码配置BroadcastReceiver,而不是通过清单。

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

经过测试并且工作正常。

答案 1 :(得分:8)

你无法通过XML捕获这些意图(我忘了为什么)。但是,您可以使用Service在其BroadcastReceiver中注册onStartCommand()成员,并在其onDestroy()中注销该成员。这将要求服务在后台运行,不断或只要您需要,所以一定要探索替代路线。

您可以在BroadcastReceiver类中定义Service,如下所示:

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

对于一个稍微复杂的示例,但显示BroadcastReceiverService如何互动的示例,请参阅我的应用程序中的CheckForScreenBugAccelerometerService,ElectricSleep。