How to restart service after the app is killed from recent tasks

后端 未结 4 734
执念已碎
执念已碎 2020-12-01 04:43

I have created a service to fetch current location of the device in periodic intervals. I want the service to run in the background even if the app is cleared from recently

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 05:21

    I use android 9 and the solution works partially for me. I had a case with foreground service (working 24/7), which I wanted to restart after the application was crashed. When the event uncaughtExceptionHandler was caught, the application got frozen besides public void onTaskRemoved(Intent rootIntent) { event doesn't work anymore in latest Android versions (I suppose O+). My application has only one activity with fragments if you need solution for more activities just use this link. To solve that problem I've added a function which checks if the activity is working (to kill it) and some instructions to kill the process:

    class MyApplication : Application() {
    
        private var currentActivity: Activity? = null
    
        override fun onCreate() {
            super.onCreate()
            StorageManager.init()
    
            this.registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
                override fun onActivityPaused(activity: Activity) {
    
                }
    
                override fun onActivityStarted(activity: Activity) {
                    currentActivity = activity
                }
    
                override fun onActivityDestroyed(activity: Activity) {
                }
    
                override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
                }
    
                override fun onActivityStopped(activity: Activity) {
                    currentActivity = null
                }
    
                override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                }
    
                override fun onActivityResumed(activity: Activity) {
                }
            })
    
            Thread.setDefaultUncaughtExceptionHandler { _, e ->
                // Close current activity
                currentActivity?.finish()
    
                val service : PendingIntent? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    // Start service in Oreo and latest
                    PendingIntent.getForegroundService(
                        applicationContext,
                        8888,
                        Intent(applicationContext, SensorService::class.java),
                        PendingIntent.FLAG_ONE_SHOT)
                } else {
                    // Start service in Nougat and older
                    PendingIntent.getService(
                        applicationContext,
                        8888,
                        Intent(applicationContext, MyService::class.java),
                        PendingIntent.FLAG_ONE_SHOT)
                }
    
                // The great solution introduced by @cgr
                val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
                alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service)
    
                // Kill the current application process to avoid freezing activity
                android.os.Process.killProcess(android.os.Process.myPid())
                exitProcess(10)
            }
        }
    }
    

    Add to manifest:

        

提交回复
热议问题