Background Service is not restarting after killed in oppo, vivo, mi android version 7.1.2
Asked Answered
D

2

6

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

Detriment answered 30/1, 2018 at 6:49 Comment(0)
M
8
  1. 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.

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

Manamanacle answered 11/5, 2018 at 11:56 Comment(7)
Please accept/upvote the answer if this solves your issueManamanacle
didn't work for me. Do know any other way we can avoid service from being killed?Pike
Kindly share your code, this answer will definitely help youManamanacle
@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 advanceOriel
have you found the solution? sorry for late responeManamanacle
@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 #61028222. Can you please take a look.Dopester
I have answered your question :)Manamanacle
G
1

To handle the Service to run continuously in the background in Chinese manufactured devices we have to use multiple ways to Cover it.

  1. Enable auto-start permissions in application settings. For auto-start code, you can use this:- [https://github.com/judemanutd/AutoStarter][1]

  2. In Chinese devices onTaskRemoved not called if you have not enabled the auto-start option in app settings.

  3. 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)
    } 
Gerah answered 23/11, 2020 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.