我已經爲一個音頻例子創建了一個簡單的Activity類,但問題是即使我離開應用程序,音頻文件仍然運行,我只想通過按任何手機中間的「確定/完成」按鈕。我應該在活動課中重寫哪種方法?'OK'按鈕的覆蓋方法
public class Audio extends Activity {
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_audio, menu);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
int resId;
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
resId = R.raw.up;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
resId = R.raw.down;
break;
default:
return super.onKeyDown(keyCode, event);
}
// Release any resources from previous MediaPlayer
if (mp != null) {
mp.release();
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
// Indicate this key was handled
return true;
}
}/// end class
接受您提問的其他問題的答案 – harshit
請參閱此鏈接..它可能有助於您解決您的查詢。 [檢查此鏈接] [1]:http://nisha113a5.blogspot.in/2012/01/intercept-home-key-android.html – user1134427