Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 912
Harish Sharma Avatar asked Jan 30 '18 06:01

Harish Sharma


2 Answers

  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

like image 137
Amin Pinjari Avatar answered Sep 28 '22 09:09

Amin Pinjari


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)
    } 
like image 44
Puneet Kumar Avatar answered Sep 28 '22 09:09

Puneet Kumar