不允许后台执行接收意图 BOOT_COMPLETED

Background execution not allowed receiving intent BOOT_COMPLETED

我读过 Android Oreo 后台执行限制,它明确指出 BOOT_COMPLETED 广播不受影响,但我无法让它在 Android Oreo 上运行.

首先,我正在针对 SDK 27 进行编译。其次,我在清单文件中声明了接收器:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <receiver
        android:name="helpers.StartDetectionAtBoot"
        android:label="StartDetectionAtBoot"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT"/>

            <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>

            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            <!--For HTC devices-->
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
            <!--For MIUI devices-->
            <action android:name="android.intent.action.REBOOT"/>
        </intent-filter>
    </receiver>

然后是receiver的实现,也可以这么简单:

public class StartDetectionAtBoot extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("test", "test");

        Intent intent0 = new Intent( context, ActivityRecognitionService.class );
        PendingIntent pendingIntent = PendingIntent.getService(context, 111, intent0, PendingIntent.FLAG_UPDATE_CURRENT);
        ActivityRecognitionClient activityRecognitionClient = ActivityRecognition.getClient(context);
        activityRecognitionClient.requestActivityUpdates(5000, pendingIntent);
    }
}

onReceive 方法未被调用,我总是会在 Android Oreo devices/emulator:

上收到 logcat 错误

W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.BOOT_COMPLETED flg=0x400010 }

阅读其他答案,他们说在清单中注册显式意图时存在一些问题,但 BOOT_COMPLETED 的情况并非如此。

都没有帮助,因为接收器根本没有被调用。

在运行时注册广播意图,让它工作(在模拟器上,从 adb 触发意图 shell),但我不确定这是正确的方法:

registerReceiver(new StartDetectionAtBoot(), new IntentFilter(Intent.ACTION_BOOT_COMPLETED));

这有什么已知的错误吗?

解决方案结合了我已经进行的两次尝试。

首先,我必须启动带有粘性通知的前台服务(即使是虚拟服务也不错):

public class StartDetectionAtBoot extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Intent intent1 = new Intent(context.getApplicationContext(), DummyService.class);
            context.startForegroundService(intent1);
        }

        Intent intent0 = new Intent( context, ActivityRecognitionService.class );
        PendingIntent pendingIntent = PendingIntent.getService(context, 111, intent0, PendingIntent.FLAG_UPDATE_CURRENT);
        ActivityRecognitionClient activityRecognitionClient = ActivityRecognition.getClient(context);
        activityRecognitionClient.requestActivityUpdates(5000, pendingIntent);
    }
}

当然,在您启动的服务中,必须有一个 onCreate 方法来创建通知并调用 startForeground

其次,我在 Android Studio 中执行了缓存失效操作,并且还擦除了模拟器实例。这部分解决方案对我来说是必需的,因为第一部分仍然没有用。

在代码中自行注册您的接收器以获得所需的隐式意图确实是开始接收该意图的正确方法。这不需要任何服务(在大多数情况下,请参见下文...)。 但您需要注意以下事项,以免在测试过程中感到困惑,也不会破坏早期的实现:

  1. 每个应用程序应进行一次自助注册运行。根据 Java/Kotlin 应用程序数据:每个静态字段生命周期一次。所以一个静态布尔字段应该让你知道:如果你需要自我注册(例如重启后或 Android 系统稍后杀死你的应用程序......)或者不需要(我引用了这个提交的工作代码:https://github.com/andstatus/todoagenda/commit/74ffc1495f2c4bebe5c43aab13389ea0ea821fde):
    private static volatile boolean receiversRegistered = false;

    private static void registerReceivers(Context contextIn) {
        if (receiversRegistered) return;

        Context context = contextIn.getApplicationContext();
        EnvironmentChangedReceiver receiver = new EnvironmentChangedReceiver();

        IntentFilter providerChanged = new IntentFilter();
        providerChanged.addAction("android.intent.action.PROVIDER_CHANGED");
        providerChanged.addDataScheme("content");
        providerChanged.addDataAuthority("com.android.calendar", null);
        context.registerReceiver(receiver, providerChanged);

        IntentFilter userPresent = new IntentFilter();
        userPresent.addAction("android.intent.action.USER_PRESENT");
        context.registerReceiver(receiver, userPresent);

        Log.i(EventAppWidgetProvider.class.getName(), "Registered receivers from " + contextIn.getClass().getName());
        receiversRegistered = true;
    }
  1. 将您的 registerReceivers 方法的调用插入到您的应用程序的 所有 个可能的入口点,以便最大限度地注册接收器,即使您的应用程序是由 [=40] 启动的=] 系统仅一次,例如:
    @Override
    public void onUpdate(Context baseContext, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        registerReceivers(baseContext);

        ...
    }
  1. 您可以让您的接收器在 AndroidManifest.xml 文件中注册相同的意图(因为这适用于 v.7 之前的 Android ...),但请注意在这种情况下,在 logcat 中,您仍然会看到 "Background execution not allowed" 以及对您的接收者的引用。这仅意味着通过 AndroidManifest.xml 注册不起作用(正如 Android 8+ 所预期的那样),但无论如何都应该调用自注册接收器!

  2. 正如我上面提到的,对于轻型小部件,通常不需要启动前台服务。此外,用户不会喜欢不断地看到您的 "widget" 在前台 运行ning 的通知(因此不断消耗资源)。唯一可能确实需要这样做的情况是 Android 过于频繁地终止您的应用程序,从而删除重新启动后完成的自注册。 我认为让你的 "widget application" 尽可能轻(需要尽可能少的内存和 CPU 资源......)是确保你的小部件应用程序仅在关键情况下才会被杀死的正确方法对于您的设备...也许您应该将您的大型应用程序一分为二,为需要不时工作的重量级应用程序制作一个类似于启动器的小部件...

W/BroadcastQueue:不允许后台执行:receiving Intent { act=android.intent.action.BOOT_COMPLETED **flg=0x400010** }

您测试来自 adb shell 的意图,可能是 am broadcast -a android.intent.action.BOOT_COMPLETED flg=0x400010 与 Android 系统在引导期间发送的 flg=0x9000010 不同。

您可以在 frameworks/base/core/java/android/content/Intent.java 找到标志定义。 flg=0x400010 没有位 FLAG_RECEIVER_FOREGROUND = 0x10000000

因此,如果您想测试来自 adb shell 的意图,您可以使用 am broadcast -a android.intent.action.BOOT_COMPLETED *--receiver-include-background.*