我使用 this media player但是当我旋转模拟器 (Ctrl+F11) 时,startPlayProgressUpdater() 无法正常工作。 旋转后如何更新搜索栏?
最佳答案
在 Activity 的 onSaveInstanceState()
回调中保存搜索栏位置。
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("current_position", seekbarPosition);
}
在您的 Activity 的 onCreate
中
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.......
if (savedInstanceState != null) {
// set seek bar position from `savedInstanceState`
}
}
更新
将可运行的通知移出启动方法。
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
public void startPlayProgressUpdater() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
if (mediaPlayer.isPlaying()) {
handler.postDelayed(notification,1000);
}else{
mediaPlayer.pause();
buttonPlayStop.setText(getString(R.string.play_str));
seekBar.setProgress(0);
}
}
然后在 onSaveInstanceState()
方法中从处理程序中删除所有回调。
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("current_position", seekbarPosition);
handler.removeCallbacks(notification);
}
关于android - android旋转后如何更新SeekBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7459853/