首页 > 解决方案 > 在 API > 26 中的 BOOT_COMPLETE 上触发作业

问题描述

我正在创建一个应用程序,该应用程序在消息(通过 MQTT 接收)到达时显示通知。无论应用程序是否正在运行,我都希望这种情况发生。
我做了一些研究,发现我可以在清单中注册一个接收器:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="anonymised">

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

        <service android:name="org.eclipse.paho.android.service.MqttService" />

        <service
            android:name="anonymised.systemservice.MqttNotificationService"
            android:icon="@drawable/ic_launcher_foreground"
            android:label="LabelForConnection"
            android:enabled="true"
            android:permission="android.permission.BIND_JOB_SERVICE" />

        <service
            android:name="anonymised.systemservice.NotificationJob"
            android:icon="@drawable/ic_launcher_foreground"
            android:label="LabelForJob"
            android:enabled="true"
            android:permission="android.permission.BIND_JOB_SERVICE" />

        <receiver
            android:name="anonymised.StartServiceReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

我看过一些文章提到隐式接收器被阻止,但是 BOOT_COMPLETED 是在 API 26 之后继续支持的文章之一。

我的 gradle 文件包含:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 26
        targetSdkVersion 28
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:support-compat:28.0.0'
}

我的代码基于:https://www.vogella.com/tutorials/AndroidTaskScheduling/article.html 这似乎表明:

package anonymised.systemservice;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class StartServiceReceiver extends BroadcastReceiver {

    private static final String TAG = "MQTT_RECI";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "ServiceReceiver started");
        SchedulerUtils.scheduleJob(context);
    }
}

然后应该触发该onReceive方法,当

~/Android/Sdk/platform-tools$ ./adb root
~/Android/Sdk/platform-tools$ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

正在运行。但是,当我运行 ADB 命令时,我的应用程序日志(与问题相关)和 system_process 日志中的以下内容一无所获:

2019-12-23 15:30:33.958 1826-1851/system_process W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.BOOT_COMPLETED flg=0x400010 } to anonymised/.systemservice.StartServiceReceiver

作为最终测试,我SchedulerUtils.scheduleJob(getApplicationContext());从我的活动中触发并关闭了应用程序。哪个有效,直到重新安排工作并且我收到:

2019-12-23 15:35:13.089 5700-5700/uk.ac.mmu.dominicsutherland.hotellock E/MAIN_REPOSITORY: MQTT Connection failed
    java.lang.IllegalStateException: Not allowed to start service Intent { cmp=anonymised/org.eclipse.paho.android.service.MqttService }: app is in background uid UidRecord{9f137b0 u0a67 TRNB idle change:uncached procs:1 seq(0,0,0)}
        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
        at android.app.ContextImpl.startService(ContextImpl.java:1532)
        at android.content.ContextWrapper.startService(ContextWrapper.java:664)
        at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:414)
        at anonymised.MainRepository.<init>(MainRepository.java:60)
        at anonymised.systemservice.NotificationJob.onStartJob(NotificationJob.java:30)
        at android.app.job.JobService$1.onStartJob(JobService.java:62)
        at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我看过:后台执行不允许接收意图 BOOT_COMPLETED但是如果可能的话,我更愿意坚持使用 AndroidManifest 和我的接收器的方法。

标签: androidbackground-processandroid-intentservicebootcompleted

解决方案


推荐阅读