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
2 Answers
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

- 2,129
- 3
- 25
- 53
-
1Please accept/upvote the answer if this solves your issue – Amin Pinjari Jul 22 '19 at 16:25
-
didn't work for me. Do know any other way we can avoid service from being killed? – Edijae Crusar Nov 11 '19 at 09:38
-
Kindly share your code, this answer will definitely help you – Amin Pinjari Dec 13 '19 at 11:51
-
@AminPinjari I have tried your suggestion, Now my app is working on one plus and MI devices, but not working on OPPO. Could you please share some sample code? Thanks in advance – Ashish Jan 24 '20 at 05:34
-
have you found the solution? sorry for late respone – Amin Pinjari Mar 29 '20 at 14:22
-
@AminPinjari I had the same problem on Oppo. I tried your solution but it didn't work. I posted all my tries and code in a new question here https://stackoverflow.com/questions/61027722/chinese-android-devices-killing-background-service. Can you please take a look. – Hamza Khurshid Apr 10 '20 at 08:27
-
I have answered your question :) – Amin Pinjari Apr 10 '20 at 08:41
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)
}

- 306
- 1
- 3