我有一组活动代码。我有一个应用程序,有超过10个活动。而且我希望它有一个不断的背景音乐,每当我去另一个活动时它就会停止。 当我在这个活动中(也在其他活动中)当我点击Emu或设备的主页按钮,然后返回应用程序时,我将“强制关闭”希望你可以帮助我。三江源
public class AdvancedKids extends Activity implements OnClickListener {
MediaPlayer yourStereo;
AssetFileDescriptor afd;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.advancelay);
View numbers = this.findViewById(R.id.button1);
numbers.setOnClickListener(this);
View alphabets = this.findViewById(R.id.button2);
alphabets.setOnClickListener(this);
View colors = this.findViewById(R.id.button3);
colors.setOnClickListener(this);
View shapes = this.findViewById(R.id.button4);
shapes.setOnClickListener(this);
View back= this.findViewById(R.id.buttonback);
back.setOnClickListener(this);
try {
// Read the music file from the asset folder
afd = getAssets().openFd("haha.mp3");
// Creation of new media player;
yourStereo = new MediaPlayer();
// Set the player music source.
yourStereo.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),afd.getLength());
// Set the looping and play the music.
yourStereo.setLooping(true);
yourStereo.prepare();
yourStereo.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onPause() {
super.onPause();
yourStereo.pause();
}
public void onResume() {
super.onResume();
yourStereo.start();
}
protected void onStop() {
super.onStop();
yourStereo.stop();
yourStereo = null;
}
@Override
public void onBackPressed(){
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
System.exit(0);
}
}).setNegativeButton("No", null).show();
}
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Intent button1 = new Intent(this, Numbers.class);
startActivity(button1);
break;
case R.id.button2:
Intent button2 = new Intent(this, Alphabets.class);
startActivity(button2);
break;
case R.id.button3:
Intent button3 = new Intent(this, Colors.class);
startActivity(button3);
break;
case R.id.button4:
Intent button4 = new Intent(this, Shapes.class);
startActivity(button4);
break;
case R.id.buttonback:
Intent buttonback = new Intent(this, MainActivity.class);
startActivity(buttonback);
break;
}
System.exit(0);
}
}
答案 0 :(得分:0)
创建服务
public class MyService extends Service implements MediaPlayer.OnPreparedListener {
private static final String ACTION_PLAY = "com.example.action.PLAY";
MediaPlayer mMediaPlayer = null;
public int onStartCommand(Intent intent, int flags, int startId) {
...
if (intent.getAction().equals(ACTION_PLAY)) {
mMediaPlayer = ... // initialize it here
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.prepareAsync(); // prepare async to not block main thread
}
}
/** Called when MediaPlayer is ready */
public void onPrepared(MediaPlayer player) {
player.start();
}
}
启动它
startService(new Intent(this, MyService.class));
了解更多here...