我的ToggleButton出現問題。Android ToggleButton暫停/恢復問題
我第一次點擊它時,它會靜音媒體聲音。第二次,它取消了它們。爲了保存ToggleButton的狀態,我使用了onPause/onResume方法。但問題是,如果我啓動活動,然後我按切換按鈕,我暫停它,當我最終恢復它,切換按鈕不再工作,應用程序保持靜音。 (即使狀態保存)
我的活動部分:
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
aManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
} else {
AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
aManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
}
@Override
public void onPause() {
super.onPause();
bundle.putBoolean("ToggleButtonState", ToggleButton.isChecked());
}
@Override
public void onResume() {
super.onResume();
ToggleButton.setChecked(bundle.getBoolean("ToggleButtonState",false));
}
我知道,我也許應該什麼地方使用另一個暫停/恢復的代碼,你能幫忙嗎?謝謝:)
這裏的日誌當我打開應用程序,打開按鈕的活動,按一下按鈕,暫停活動,恢復,請再次點擊該按鈕,暫停再次活動:
01-01 04:19:55.004: D/OpenGLRenderer(10766): Enabling debug mode 0
01-01 04:19:55.113: I/ActivityManager(10766): Timeline: Activity_idle id: [email protected] time:9273361
01-01 04:20:00.902: I/ActivityManager(10766): Timeline: Activity_launch_request id:com.example.appquiz time:9279155
01-01 04:20:01.121: I/ActivityManager(10766): Timeline: Activity_idle id: [email protected] time:9279369
01-01 04:20:05.840: I/ActivityManager(10766): Timeline: Activity_idle id: [email protected] time:9284092
01-01 04:20:09.988: I/ActivityManager(10766): Timeline: Activity_launch_request id:com.example.appquiz time:9288234
01-01 04:20:10.168: I/ActivityManager(10766): Timeline: Activity_idle id: [email protected] time:9288414
01-01 04:20:20.566: I/ActivityManager(10766): Timeline: Activity_idle id: [email protected] time:9298814
您是否嘗試過在您的onresume和暫停方法中輸出日誌以查看該卡在靜音時的值,您擁有的值編輯,仍然是你退出的價值? –
你如何獲得捆綁? –
Bundle:private static Bundle bundle = new Bundle(); –