1
要更新一个seekbar,我使用下面的代码: 我的问题是,seekBar.setProgress()调用时,UI上的其他元素会冻结,所以我想要一个不同的线程更新主线程中的seekBar。Android:线程更新UI
如何继续?
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;
}
http://samir-mangroliya.blogspot.in/p/android-asynctask-example.html –
或者只是把它放在了Runnable,而且发表它。请参阅处理程序的后处理方法。 – nullpotent
我不认为调用seekBar.setProgress()只能冻结UI。而UI线程只存在一个。 – ATom