【问题标题】:ACTION_SCREEN_ON and ACTION_SCREEN_OFF not working?ACTION_SCREEN_ON 和 ACTION_SCREEN_OFF 不起作用?
【发布时间】:2011-11-14 01:30:26
【问题描述】:

我正在尝试在屏幕关闭(锁定)时关闭 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_ONACTION_SCREEN_OFF 从未被解雇! USER_PRESENTBOOT_COMPLETED 工作正常,但另一个没有。我使用的是模拟器,而不是真实设备 - 这会导致问题吗?

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

提前致谢

【问题讨论】:

标签: android android-emulator android-manifest


【解决方案1】:

要捕获 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,对于 IntentFilter,您使用 ON,而对于操作,您使用 OFF?
  • 两者都是设置到 IntentFilter 的操作。
  • 我已经以这种方式添加,但是当我清除最近的应用程序时,BroadcastReceiver 的 onReceive 方法没有响应。如果应用程序正在运行,则 onReceive 工作正常。
【解决方案2】:

您无法通过 XML 捕捉到这些意图(我忘记了原因)。但是,您可以使用Service 在其onStartCommand() 中注册一个BroadcastReceiver 成员并在其onDestroy() 中取消注册它。这将要求该服务在后台持续运行,或者只要您需要它就一直运行,因此请务必探索替代路线。

您可以像这样在Service 类中定义BroadcastReceiver

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 如何交互,请参阅我的应用 ElectricSleep 中的 CheckForScreenBugAccelerometerService

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多