2

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.

user9924189
  • 101
  • 1
  • 13
  • Why do you have a service in the first place? The point behind a service is to do work when your UI is *not* in the foreground. If you only want the work to be done while your app is in the foreground, do not use a service. Use an ordinary Java object. To find out when your app comes and goes from the foreground, consider `ProcessLifecycleOwner`. – CommonsWare Jun 11 '18 at 12:31

4 Answers4

1

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

user9924189
  • 101
  • 1
  • 13
0

According with the Activity lifecicle you should use onStop() to stop your audio from beign 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();
        }
}
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0

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

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

onPause() - when any pop-up appears

Chethan Kumar
  • 185
  • 1
  • 12
0

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().

Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105