I encounter strange behavior with my background Servicerunning on Android O.
In my example application is used targetSdkVersion 26
I have a simple service that just prints out some status information and needs to be recreated with 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 starts as part of the Activity:
startService(ServiceTest.buildIntent(applicationContext))
When I started the service and leave the application after that, by clicking the "Back" button, everything will work 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
, ; , "", , , , , , (- START_STICKY). Android - . , , .
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
- ?