后台执行不允许接收意图BOOT_COMPLETED [英] Background execution not allowed receiving intent BOOT_COMPLETED

查看:224
本文介绍了后台执行不允许接收意图BOOT_COMPLETED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经了解了Android Oreo后台执行限制,并且清楚地指出BOOT_COMPLETED广播不受影响,但是我无法使其在Android Oreo上正常工作.

I've read about Android Oreo background execution limitations, and it clearly states that BOOT_COMPLETED broadcast is unaffected, but I can't get it to work on Android Oreo.

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

First, I am compiling against SDK 27. Secondly, I declared the receiver inside the manifest file:

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

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

Then there's the implementation of the receiver, which can also be simple as that:

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错误:

onReceive method is not called and I will always get logcat error on Android Oreo devices/emulator:

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

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

在阅读其他答案时,他们说在清单中注册显式意图时存在一些问题,但BOOT_COMPLETED一个不是这种情况.

Reading other answers, they said there were some problems when registering explicit intents in the manifest but this is not the case of BOOT_COMPLETED one.

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

Neither this helped because the receiver is not called at all.

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

Registering broadcast intent at runtime, get it to work (on the emulator, firing the intent from adb shell), but I'm not sure it's the right way to do it:

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

有任何已知的错误吗?

推荐答案

解决方案是我已经进行过两次尝试的组合.

The solution was a combination of two attempts I had already made.

首先,我必须使用粘性通知启动前台服务(即使是虚拟服务也可以):

First, I had to start a foreground service (even a dummy service would be good) with sticky notification:

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.

Of course inside the service you start there must be an onCreate method which creates a notification and calls startForeground.

其次,我在Android Studio中进行了缓存失效,并且还擦除了模拟器实例.解决方案的这一部分对我来说是必需的,因为第一部分仍然无法正常工作.

Secondly, I had did a cache invalidation in Android Studio and I also wiped emulator instance. This part of the solution was necessary for me since the first part still didn't work.

这篇关于后台执行不允许接收意图BOOT_COMPLETED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆