Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity and JobIntentService Lifecycle

I am running a JobIntentService to perform a task in background. The reason for using JobIntentService is that so that the user can minimize the screen while the operation is happening and even if the Android OS destroys the activity the JobIntentService will still keep running and if the user comes back he can be updated with the results.

But I have a case in which suppose the user himself closes the app then I want to stop the JobIntentService also.

Is there any way to notify the JobIntentService when the user himself closes the app.

Any Help will be really appreciated.

EDIT: I tries using onTaskRemoved by that is of no use aw my Test device does not call it(I am sure many other device would not be calling it either). Also The service is automatically stopped when the app is removed from the recent list but is then again restarted when the app is opened again which is what I don't want.

like image 256
Rajesh K Avatar asked Mar 06 '23 17:03

Rajesh K


1 Answers

The reason that your Service is killed when the user swipes the task from the recent tasks list is that Android just kills the OS process hosting the application when the user swipes the task from the recent tasks list. Android will then restart any Service that was running in the OS process that it has just killed.

The behaviour of this has changed over time and is different on different version of Android and also some manufacturers have their own custom ROMs where they implement this in different ways.

There is nothing you can do that will ensure that your Service will not get killed and restarted by Android. You can try this, which may help:

Put your Service in a separate OS process. To do this add android:process=":remote" to the <service> tag in the manifest. On some versions of Android, swiping the task from the list of recent tasks only kills the OS process hosting the activities. This may prevent Android from killing your Service in this case.

You also say that onTaskRemoved() is not called and that your Service is not automatically restarted after Android kills it. This happens on some devices where the manufacturer is trying to aggressively save battery power. This is common on Chinese devices (Xiaomi, ZTE, etc.) and some LG, Huawei and Lenovo devices also show this behaviour. On these devices there is a list of applications that are allowed to run in the background (also known as "protected" apps). You must manually add your app to this list on these devices (or tell your users that they need to manually add your app to the list). If your app is in the list of "protected apps" or "apps allowed to run in the background", then Android might now kill off your Service if the user swipes the task from the list of recent tasks, or if it does kill it, it should call onTaskRemoved() and it should also automatically restart the Service immediately.

like image 170
David Wasser Avatar answered Mar 15 '23 23:03

David Wasser