Android stop service when exit the app

后端 未结 3 1443
甜味超标
甜味超标 2021-01-02 01:10

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

相关标签:
3条回答
  • 2021-01-02 01:31

    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.

    0 讨论(0)
  • 2021-01-02 01:42

    put this line in yout activity

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

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

    0 讨论(0)
  • 2021-01-02 01:44

    Override the method onUnbind(Intent intenet) in Service

    0 讨论(0)
提交回复
热议问题