Reputation: 21
I am running a foreground service which is doing some work in the background while the user is interacting with my application.The problem which occurs is that when the user comes out of the application and kills the application from the home screen the service is not destroyed.Is there any way to kill the service when my application dies.
Upvotes: 2
Views: 4217
Reputation: 480
I solved it by calling stopService(intent);
in my MainActivity where I call the service from another Class.
@Override
protected void onDestroy()
{
super.onDestroy();
stopService(intent);
}
But as For the yourservice.class needed to have the onDestroy
method, it is crucial.
@Override
public void onDestroy()
{
super.onDestroy();
stopForeground(true);
stopSelf();
}
Upvotes: 0
Reputation: 4710
Just call stopService() for your service in onDestroy() method of your main Activity.
Upvotes: 0
Reputation: 539
With "kills the application from the home screen" I'm assuming you mean swiping away the app from the task manager? In that case, there is a convenient callback in the Service class that lets you handle this.
Simply override that method and call stopSelf()
.
Upvotes: 6
Reputation: 196
why do you make the service foreground? If it's only doing work while the user interacts with your application, maybe you should not keep it foreground.
Upvotes: 0
Reputation: 973
You can do following to stop the service
call stopService
method in onDestroy
method of activity
hope this will solve your problem
Upvotes: 3