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

前端 未结 2 766
萌比男神i
萌比男神i 2020-12-03 06:15

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 i

2条回答
  •  佛祖请我去吃肉
    2020-12-03 07:06

    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)
        } 
    

提交回复
热议问题