Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

android - When I exit an application the sound runs on the background

I "created" an application and when I go back or I press the home button to exit from the application, the audio from my application is still running! I have to close all running apps to make it stop.

How can I stop the audio running when I press the home button?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...