user9924189
user9924189

Reputation: 101

How to Stop service when app goes in background

I have a multi activity app. In the main activity a service is initiated that plays music. When I navigate through activities the music is still playing ( which is something that I want) but the music is still playing when I click home button and app goes in the background(which is something I don't want).

  1. My first solution was to do 'stopService()' onPause of main activity but this prevented the music from playing in the other activities.

  2. Tried the same in onStop method, same problem occurred.

Q: How can I stop the music(stop the service) from playing when the whole app goes in the background?

My service code:

public class MediaService extends Service {
private MediaPlayer player;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    player = MediaPlayer.create(this, R.raw.music);
    player.setLooping(true);
    player.start();
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();

    player.stop();
}

And I start/stop service with:

 music_intent = new Intent(this, MediaService.class);
 startService(music_intent);
 stopService(music_intent);

P.S. Thanks for all the answers but as I said onStop methods stops the music when I change activities which is something that I don't want.

Upvotes: 2

Views: 2114

Answers (4)

Gastón Saillén
Gastón Saillén

Reputation: 13159

According with the Activity lifecycle you should use onStop() to stop your audio from being played.

enter image description here

Remember that onStop()

May never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

So it should solve your problem doing this in this way:

@Override
public void onStop() {
    super.onStop();

     if(player.isPlaying()){
        player.stop();
        player.release();
        }
}

Upvotes: 0

user9924189
user9924189

Reputation: 101

Found this solution in a similar question that uses Application.ActivityLifecycleCallbacks in Application class to check when app goes in Background and then send a Broadcast to the service to stop it.

More in: Stop MediaPlayer service when app goes in Background

Upvotes: 1

Farhana Naaz Ansari
Farhana Naaz Ansari

Reputation: 7948

Here is the actual answer, try this. For some reason, the service is not stopping when the app goes to background

It stopped long before your stopService() call, as it stopped once onHandleIntent() returned, milliseconds after it was created.

What is not stopping is your Timer, which runs on a background thread and will continue running until you cancel it or your process terminates.

IMHO, this is an inappropriate use of IntentService. If you want to control the lifespan, use a Service, and stop background work in onDestroy().

Upvotes: 0

Chethan Kumar
Chethan Kumar

Reputation: 195

use onStop() or onPause() to stop the service rather than onDestroy();

onStop() - when app goes backgroun(not visible)

onPause() - when any pop-up appears

Upvotes: 0

Related Questions