mushroom
mushroom

Reputation: 1945

How do I end a service when the Android application closes?

I understand that when you call an Intent to start a service, the service continues until it is explicitly ended.

I would like the service to persist multiple activities. How do I end it when all activities of the application is destroyed?

Or does it get destroyed automatically?

Upvotes: 1

Views: 470

Answers (1)

Green goblin
Green goblin

Reputation: 9996

In the main Activity, override onDestroy and call stopService

@Override
public void onDestroy()
{
    super.onDestroy();
    Toast.makeText(<Context>, "Stopping service", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(<MainActivity>.this, <YourService>.class);
    stopService(intent);
}

Upvotes: 2

Related Questions