I am developing android application which has background running Service.
When I swap out app from the "Recent app List", it will cause the application to shutdown and stop the service. (The following method has the code for the same.)
@Override
public void onTaskRemoved(Intent rootIntent)
{
//code to be executed
//Stop service
super.onTaskRemoved(rootIntent);
}
Service start up code is in Application class(onCreate()), it will never be executed if app gets resume.
Positive Scenario
1) If I relaunch app after successful execution of service, new instance of app will be created and service will also start.
Negative Scenario
1) Because there is some code in the above method which is responsible to stop the thread and the service, it causes the app to take some time to stop the service (after swapping from the recent apps). During this time if I relaunch the application, the application resumes instead off getting recreated. Now, the service which was running, will stop.
So,in this type situation I have application but without background service.
How can I handle this situation?
1) Application shouldn't be re-launch until service's task is completed. 2) Start service from launcher activity.
Thanks in advance.