BroadcastReceiverOnBootComplete.java
public class BroadcastReceiverOnBootComplete extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, AndroidServiceStartOnBoot.class);
context.startService(serviceIntent);
}
}
}
AndroidServiceStartOnBoot.java
public class AndroidServiceStartOnBoot extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
// here you can add whatever you want this service to do
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javacodegeeks.androidserviceonbootexample">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<receiver
android:name="com.javacodegeeks.androidserviceonbootexample.BroadcastReceiverOnBootComplete"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<service android:name="com.javacodegeeks.androidserviceonbootexample.AndroidServiceStartOnBoot"></service>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.javacodegeeks.androidserviceonbootexample.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest
>
if we want to be sure our service is sticky
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (workerThread == null) {
workerThread = Thread(run)
workerThread!!.start()
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
return Service.START_STICKY
}
and
override fun onTaskRemoved(rootIntent: Intent?) {
val restartService = Intent(applicationContext,
this.javaClass)
restartService.`package` = packageName
val restartServicePI = PendingIntent.getService(
applicationContext, 1, restartService,
PendingIntent.FLAG_ONE_SHOT)
val alarmService = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000, restartServicePI)
}
References
http://blog.vogella.com/2011/12/11/automatically-starting-services-in-android-after-booting/
https://examples.javacodegeeks.com/android/core/activity/android-start-service-boot-example/
http://www.framentos.com/en/android-tutorial/2012/08/01/how-to-start-a-service-at-boot-in-android/