-1

How to make service (background music) to turn off when I close the app or turn off the screen, but not when I switch to another activity?

Currently Music plays fine, I lasts until the screen turn off or I close the app, but also close when I switch to another activity. It is probably caused by onStop and onPause assigned to every activity. Please help me solve this issue

My service:

public class MyService extends Service {


private final String TAG = null;
MediaPlayer player;

public IBinder onBind(Intent arg0) {

    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    player = MediaPlayer.create(this, R.raw.music;
    player.setLooping(true); // Set looping
    player.setVolume(100, 100);




}

public int onStartCommand(Intent intent, int flags, int startId) {
    player.start();
    return 1;
}

public void onStart(Intent intent, int startId) {
    // TO DO
}

public IBinder onUnBind(Intent arg0) {
    // TO DO Auto-generated method
    return null;
}


protected void onStop() {
    player.pause();
}

public void onPause() {
    player.pause();
}

@Override
public void onDestroy() {
    player.stop();
    player.release();
}

@Override
public void onLowMemory() {

}

}

My example activity has this:

public void onPause() {
    super.onPause();
    stopService(new Intent(a.this, MyService.class));



}

public void onStop() {
    super.onStop();
    stopService(new Intent(a.this, MyService.class));

}

public void onResume() {
    super.onResume();
    startService(new Intent(a.this, MyService.class));

}

public void onRestart() {
    super.onRestart();
    startService(new Intent(a.this, MyService.class));

}
Old York
  • 73
  • 1
  • 9

2 Answers2

0

There are two ways i can suggest

1) let it be in onPause(), create a flag/boolean that will set to false by default and will be true in every-case such as new activity startup, on back press etc. so if onPause is called and flag is false you can stop the music.

2) you have background service already, you can keep checking which activity is in foreground, if is homeScreen you can stop the music.

Edit :- to know if your app is not in the foreground

check this link

this is good example

Community
  • 1
  • 1
siddhesh
  • 563
  • 1
  • 9
  • 15
0

Use this code.

Remove this line:

player.pause();

public class MyService extends Service {


private final String TAG = null;
MediaPlayer player;

public IBinder onBind(Intent arg0) {

return null;
}

@Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.music;
player.setLooping(true); // Set looping
player.setVolume(100, 100);
}

public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}

public void onStart(Intent intent, int startId) {
// TO DO
}

public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}

protected void onStop() {
//player.pause();
}

public void onPause() {
//player.pause();
}

@Override
public void onDestroy() {
//player.stop();
player.release();
}

@Override
public void onLowMemory() {
 }
}
Sikander Bhutto
  • 226
  • 1
  • 11