I know it is too late to share the answer but I will put my two cents here because it is very very important. I wasted my 2 days in digging this problem out. I tried all the suggested solutions as provided here but nothings seemed to be working. Below is the solution I implemented in following steps:
Step # 01
Create your foreground service as you are doing and register it accordingly in manifest. For sample purposes, I am sharing sample of service.
class MyService : Service() {
private var wakeLock: PowerManager.WakeLock? = null
override fun onBind(intent: Intent): IBinder? {
Log.d(tag!!, "Some component want to bind with the service")
// We don't provide binding, so return null
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d(tag!!, "onStartCommand executed with startId: $startId")
// by returning this we make sure the service is restarted if the system kills the service
return START_STICKY
}
override fun onCreate() {
super.onCreate()
Log.d(tag!!, "The service has been created".toUpperCase(Locale.ROOT))
startForeground(1, NotificationUtils.createNotification(this))
acquireLock()
}
override fun onDestroy() {
super.onDestroy()
Log.d(tag!!, "The service has been destroyed".toUpperCase(Locale.ROOT))
Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show()
}
override fun onTaskRemoved(rootIntent: Intent?) {
Log.d(tag!!, "onTaskRemoved")
val restartServiceIntent = Intent(applicationContext, this.javaClass)
restartServiceIntent.setPackage(packageName)
val restartServicePendingIntent = PendingIntent.getService(applicationContext, 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT)
val alarmService = applicationContext.getSystemService(ALARM_SERVICE) as AlarmManager
alarmService[AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000] = restartServicePendingIntent
super.onTaskRemoved(rootIntent)
}
@SuppressLint("WakelockTimeout")
private fun acquireLock() {
// we need this lock so our service gets not affected by Doze Mode
wakeLock =
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyService::lock").apply {
acquire()
}
}
}
}
NOTE: I have covered all the possible use cases to restart the Service in case it gets killed by OS. There is one thing left if user restarts cell phone. This case can be found via other stackoverflow answers very easy. In Broadcast one just needs to start Service.
Step # 02
Make an application and register in manifest. And add below line of code in your application class.
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
val receiver = ComponentName(this, MyService::class.java)
val pm = packageManager
pm.setComponentEnabledSetting(
receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)
}
}
Here, MyService is the component name and this can be Service or Broad cast Receiver you are already using in you app. In my case I trie with Android service
Now, this is the time to register this Application class in Manifest file. Open the manifest file and in application tag use property name and place your application class name which was just created MyApplication.
Step # 03
There is no third step. You are done. You just install the apk and by this way Service will not killed even the app is killed. I tested the above solution on Vivo device and it worked
NOTE: In case, above solution does not work, please check the manifest file for allowBackup property if you find out this property in manifest file just remove it and uninstall the app and then install the app it will work for sure and then you can set that property again.