I'm in the process of creating an app which displays a notification whenever a message (received via MQTT) arrives. I'd like this to happen regardless of whether the app is running or not.
I've done a bit of research and found that I can register a receiver in my manifest:
<?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>
I've seen a few articles which have mentioned that implicit receivers are being blocked, however BOOT_COMPLETED was one of the ones to continue being supported post API 26.
My gradle file contains:
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'
}
My code is based on: https://www.vogella.com/tutorials/AndroidTaskScheduling/article.html Which seems to indicate that:
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);
}
}
Should then trigger the onReceive
method, when
~/Android/Sdk/platform-tools$ ./adb root
~/Android/Sdk/platform-tools$ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
is run. However, when I run the ADB commands I get nothing in my application logs (relating to the issue) and the following in the system_process logs:
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
As a final test, I triggered SchedulerUtils.scheduleJob(getApplicationContext());
from my activity and closed the app. Which worked, until the job was rescheduled and I received:
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)
I've had a look at: Background execution not allowed receiving intent BOOT_COMPLETED however I'd prefer to stick with the method using the AndroidManifest and my receiver if possible.