Override the onPause() method of all your activities, and turn off/pause/stop the music when it's called. You might want use a boolean flag to check if you're moving within your app, a scenario in which you'd want the music to continue:
boolean movingInApp = false;
....
movingInApp = true;
Intent intent...
.....
public void onPause() {
if(!movingInApp) {
//stop service
}
}
public void onResume() {
movingInApp = false;
//Rest of your code
}
By setting the value of movingInApp
to true
before launching any intent etc, you can prevent your app from stopping the music. Remember to set it to false
again later in your onResume()
method. If the system makes your app go to the background, this will be false, and your music will be stopped.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…