Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android O: Service not stopped by background execution limits

I encounter a strange behaviour with my background Service running on Android O .

My sample app uses targetSdkVersion 26

I have a simple service, which just prints out some state information and shall be recreated using START_STICKY:

class ServiceTest : Service() {

companion object {
    private val TAG = "ServiceTest"

    fun buildIntent(context: Context): Intent {
        return Intent(context, ServiceTest::class.java)
    }
}

override fun onBind(p0: Intent?): IBinder? {
    return null
}


override fun onCreate() {
    super.onCreate()
    Log.d(TAG, "onCreate")
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    Log.d(TAG, "onStartCommand: startId = " + startId)
    return START_STICKY
}

override fun onDestroy() {
    Log.d(TAG, "onDestroy")
    super.onDestroy()
}

}

The service is started within an Activity:

startService(ServiceTest.buildIntent(applicationContext))

When I start the service and leave the app afterwards by pressing the back button, everything works as expected:

08-17 16:30:25.182 8980-8980/de.continental.android.androidoservicetest D/ServiceTest: onCreate
08-17 16:30:25.184 8980-8980/de.continental.android.androidoservicetest D/ServiceTest: onStartCommand: startId = 1
08-17 16:31:26.799 900-924/? W/ActivityManager: Stopping service due to app idle: u0a141 -1m1s629ms de.continental.android.androidoservicetest/.ServiceTest
08-17 16:31:26.804 8980-8980/de.continental.android.androidoservicetest D/ServiceTest: onDestroy

But, if I start the service; leave the app by pressing back button and finally remove the app from the recent app list by swiping it away, my service is restarted as expected (due to START_STICKY). But the service is not killed by Android OS due to Background operation limits after a defined period of time. It seems that my service is continously running and the OS does not stop it.

08-17 16:23:13.090 8914-8914/de.continental.android.androidoservicetest D/ServiceTest: onCreate
08-17 16:23:13.091 8914-8914/de.continental.android.androidoservicetest D/ServiceTest: onStartCommand: startId = 1
08-17 16:23:18.600 900-3234/? W/ActivityManager: Scheduling restart of crashed service de.continental.android.androidoservicetest/.ServiceTest in 1000ms
08-17 16:23:19.635 900-924/? I/ActivityManager: Start proc 8980:de.continental.android.androidoservicetest/u0a141 for service de.continental.android.androidoservicetest/.ServiceTest
08-17 16:23:20.158 8980-8980/? D/ServiceTest: onCreate
08-17 16:23:20.160 8980-8980/? D/ServiceTest: onStartCommand: startId = 3

Has anyone faced the same problem or has an explanatation?

like image 767
Christopher Avatar asked Aug 17 '17 14:08

Christopher


Video Answer


1 Answers

I think it's an actual bug, have a look: https://issuetracker.google.com/issues/64535598

like image 110
Ido Sofi Avatar answered Oct 10 '22 08:10

Ido Sofi