首页 > 解决方案 > 后台执行不允许接收意图 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>

然后是接收器的实现,也可以很简单:

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 设备/模拟器上得到 logcat 错误:

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

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

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

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

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

有任何已知的错误吗?

标签: androidbroadcastreceiverandroid-8.0-oreoandroid-background

解决方案


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

  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 方法调用插入应用程序的所有可能入口点,以最大限度地增加注册接收器的机会,即使您的应用程序仅由 Android 系统启动一次,例如:
    @Override
    public void onUpdate(Context baseContext, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        registerReceivers(baseContext);

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

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


推荐阅读