13

I'm creating a game application with background music.I used Android Service to play the background music because I wanted to run BGM while changing activities. my problem is,I have declared finish() in onPause method in each activity(I don't want to let the user to return & want to kill the activity).

so when I intent to other activity it calls onDestroy and stops the service. I want to stop the service exactly exit the app( pressing the home button )and want to go through activities with BGM and finish() in onPause().is this possible? or is there another solution?

public class BackgroundMusicService extends Service {
    private static 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.topbgm);
        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;
    }

    public void onStop() {

    }

    public void onPause() {

    }

    @Override
    public void onDestroy() {
        player.stop();
        player.release();
        Log.i("service", "service killed");
    }

    @Override
    public void onLowMemory() {

    }
}

in manifest

<service android:name=".BackgroundMusicService" android:process=":remote" />
Hassy31
  • 2,793
  • 3
  • 21
  • 37

3 Answers3

17

put this line in yout activity

stopService(new Intent(this, BackgroundMusicService.class));

in onDestroy() method where you pressing the home button.

kyogs
  • 6,766
  • 1
  • 34
  • 50
  • thanks for the reply.I did this.if you read my question carefully, the problem is when activity changes,finish() calls onDestroy method and stops the service.do you have any idea? – Hassy31 Feb 13 '13 at 06:14
  • I am having a scenario that at times the service is still running even if I remove it from the recent menu in android, could you guys have any idea to this? – kinsley kajiva Aug 14 '19 at 12:20
2

stopService(new Intent(this, BackgroundMusicService.class));

add this to your onPause() method and onDestroy() method in your main activity. Because If you press the Home button the app will be on background for random time and onDestroy() method would not invoked as soon as you minimize the app. The best way to do it is putting it in to the onPause() method. onPause() method is invoked when your application activity is not the fore-ground activity.

Sathirar
  • 331
  • 4
  • 17
  • thank you for reply.I tried this.thing is I want to run the service while going through activities.but I have finish() in onPause & it invokes onDestroy method – Hassy31 Feb 13 '13 at 06:17
2

Override the method onUnbind(Intent intenet) in Service

Imeshke
  • 811
  • 6
  • 13