简体   繁体   中英

BroadcastReceiver is not called in Android >= Api 26

BroadcastReceiver ist not working for any action in Api 26 or higher, according to documentation implicit actions are blocked, but not boot_completed. I tested with Android 19, 24, 26 and 28.

Here is my receiver declaration:

<receiver
        android:name="de.com.limto.limto1.broadcastReceiver"
        android:label="StartMyServiceAtBootReceiver"
        android:directBootAware="true"
        android:enabled="true">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
            <action android:name="android.intent.action.REBOOT" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.intent.action.ACTION_SHUTDOWN" />
            <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

and here my permissions

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

and here my receiver:

@Override
public void onReceive(Context context, Intent intent) {
    try {
        Log.d(BROADCAST_RECEIVER,intent.getAction());
        lib.appendLog(intent.getAction(),context);
        lib.ShowToast(context, BROADCAST);
        if (Intent.ACTION_SHUTDOWN.equals(intent.getAction())) {
            if (service != null) {
                try {
                    service.stop();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                    Log.e(BROADCAST_RECEIVER,throwable.getMessage(), throwable);
                }
            }
        } else {
            /*if (service != null) return;
            Intent serviceIntent;
            serviceIntent = new Intent(context, LimindoService.class);
            serviceIntent.putExtra(BOOT, true);
            */
            LimindoService.ccontext = context;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //context.startService(serviceIntent);
                lib.scheduleJob(context);
            } else {
                //context.startService(serviceIntent);
                startServiceByAlarm(context);
            }


        }
    }
    catch (Throwable ex)
    {
        ex.printStackTrace();
        lib.appendLog(ex.getMessage(), context);
        Log.e(BROADCAST_RECEIVER,null,ex);
        lib.ShowToast(context, ex.getMessage());
    }
}

AFAIK BOOT_COMPLETED广播没有类别android.intent.category.DEFAULT ,删除类别应该可以解决问题。

此行为仅发生在 api >= 26 的某些设备上......这是由手机设置引起的:在设置中禁用了启动时启动服务。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM