Android stop service when exit the app

后端 未结 3 1460
甜味超标
甜味超标 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.

提交回复
热议问题