我使用以下代碼更新一個seekbar。問題是每當seekBar.setProgress()被調用時,UI上其它元素就被凍結。因此我想用另外一個線程在主線程中來更新 seekBar。
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
int pos;
switch (msg.what) {
case SHOW_PROGRESS:
pos = setProgress();
if (!mDragging && mBoundService.isPlaying()) {
msg = obtainMessage(SHOW_PROGRESS);
sendMessageDelayed(msg, 100 - (pos % 1000));
}
break;
}
} catch (Exception e) {
}
}
};
private int setProgress() {
if (mBoundService == null || mDragging) {
return 0;
}
int position = mBoundService.getCurrentPosition();
int duration = mBoundService.getDuration();
if (sliderSeekBar != null) {
if (duration > 0) {
// use long to avoid overflow
long pos = 1000L * position / duration;
sliderSeekBar.setProgress((int) pos);
}
}
if (sliderTimerStop != null)
sliderTimerStop.setText(stringForTime(duration));
if (sliderTimerStart != null)
sliderTimerStart.setText(stringForTime(position));
return position;
}
我不明白為什麼會出現這樣的問題,如何進行?
sendMessageDelayed(msg, 100 - (pos % 1000));
更新頻率太快,你應該是寫錯了吧?
sendMessageDelayed(msg, 1000 - (pos % 1000));