当按下主页按钮时如何暂停背景音乐

时间:2012-12-23 14:37:04

标签: android

我需要知道这一点,所以我可以删除退出选项。

 public void onWindowFocusChanged(boolean hasFocus) { 
if (hasFocus) { 
 mediaPlayer = MediaPlayer.create(this, R.raw.theme); 
 mediaPlayer.start();
    mediaPlayer.setLooping(true);

}}

1 个答案:

答案 0 :(得分:0)

public void onWindowFocusChanged(boolean hasFocus) { 
if (hasFocus) { 
 Static mediaPlayer mp = MediaPlayer.create(this, R.raw.theme); 
mp.start();
mp.setLooping(true);

}}

使用此选项覆盖homekey。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
    Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();                     
    mp.pause();
    return true;
}
return super.onKeyDown(keyCode, event);
}