Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android service not restarted when app back from idle state

I've upgraded my app to API 26 and I'm facing some issues with the new background execution limits.

On Oreo devices, as soon as my app goes in background, my app is stopped by the OS due to idle state as written in logs :

Stopping service due to app idle: u0a80 com.example.test/com.example.test.service1
Stopping service due to app idle: u0a80 com.example.test/com.example.test.service2

Then, I try to start my app again and the last activity is correctly restored (including its fragment, and all data displayed)

07-16 10:21:51.253 system_process I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.test/com.example.test.activity1 bnds=[317,1159][586,1508]} from uid 10024

The issue is that my services are not restarted.

They both returns START_STICKY in the onStartCommand which should tell the OS that the service should be restarted after being killed as it is state in the documentation :

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service.

Any Idea what I'm doing wrong ?

Is it possible to restore the services without explicit calls to startService or bindService methods ?

like image 786
nbe_42 Avatar asked Jul 16 '18 08:07

nbe_42


1 Answers

Starting from Android O there is limitation on how freely app can access background Service. If your app is not running is foreground then the Services are terminated by OS, as if stopSelf() is called. That is the reason why START_STICKY won't rescue in this case.

You need to either restart the Service when Activity is created or create ForegroundService and execute until your task is completed.

like image 139
Sagar Avatar answered Sep 30 '22 15:09

Sagar