I want to run the service in the background even if the app is killed. This functionality is working fine on some devices. But in oppo, mi and Vivo phone it's not running if the app is killed. how can I do this for these devices
I had same issue with Oppo, Vivo, Mi and etc phones, after removing from recent applications app was getting killed even services was getting killed
Solution: I had add autostart permissions like this in my application and it worked.
After resolving this issue my app was getting frozen/killed after sometime running in background due to DOZE mode
Solution: for this condition this worked and now my app is working in background in any device
To handle the Service to run continuously in the background in Chinese manufactured devices we have to use multiple ways to Cover it.
Enable auto-start permissions in application settings. For auto-start code, you can use this:- [https://github.com/judemanutd/AutoStarter][1]
In Chinese devices onTaskRemoved not called if you have not enabled the auto-start option in app settings.
onTaskRemoved in Chinese devices will be called only after you allow auto-start permissions.
In onTaskRemoved of Service add this code snippet:-
override fun onTaskRemoved(rootIntent: Intent?) {
log("onTaskRemoved is called::")
val restartServiceTask = Intent(applicationContext, EndlessService::class.java)
restartServiceTask.setPackage(packageName)
restartServiceTask.action = Actions.START.toString()
val pendingIntent = PendingIntent.getService(this, 1, restartServiceTask, PendingIntent.FLAG_ONE_SHOT)
val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
alarmManager[AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 1000] =
pendingIntent
super.onTaskRemoved(rootIntent)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With