安卓:从线程更新界面 [英] Android: update UI from thread

查看:137
本文介绍了安卓:从线程更新界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要更新一个搜索栏,我用下面的code:
我的问题是,任何时候,seekBar.setProgress()调用是,在UI变得冻结等元素,所以我想有一个不同的线程,在主线程更新搜索栏。

如何进行?

 专用处理器mHandler =新的处理程序(){
    @覆盖
    公共无效的handleMessage(消息MSG){
        尝试{
            INT POS;
            开关(msg.what){
            案例SHOW_PROGRESS:
                POS = setProgress();
                如果(mDragging&放大器;!&放大器; mBoundService.isPlaying()){
                    味精= obtainMessage(SHOW_PROGRESS);
                    sendMessageDelayed(味精,100 - (POS%1000));
                }
                打破;
            }
        }赶上(例外五){        }
    }
};私人诠释setProgress(){
    如果(mBoundService == NULL || mDragging){
        返回0;
    }
    INT位置= mBoundService.getCurrentPosition();
    INT持续时间= mBoundService.getDuration();
    如果(sliderSeekBar!= NULL){
        如果(持续时间大于0){
            //使用长以避免溢出
            长POS = 1000L *位置/持续时间;
            sliderSeekBar.setProgress((INT)POS);
        }
    }    如果(sliderTimerStop!= NULL)
        sliderTimerStop.setText(stringForTime(持续时间));
    如果(sliderTimerStart!= NULL)
        sliderTimerStart.setText(stringForTime(位置));    返回的位置;
}


解决方案

活动有<一个href=\"http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29\"相对=nofollow> runOnUiThread 方法,它允许不同的线程更新UI组件。你的 setProgress 方法最终会看起来像:

 私人诠释setProgress(){    如果(mBoundService == NULL || mDragging){
        返回0;
    }
    最终诠释位置= mBoundService.getCurrentPosition();
    最终诠释持续时间= mBoundService.getDuration();    runOnUiThread(新的Runnable(){        @覆盖
        公共无效的run(){            如果(sliderSeekBar!= NULL){
                如果(持续时间大于0){
                    //使用长以避免溢出
                    长POS = 1000L *位置/持续时间;                    sliderSeekBar.setProgress((INT)POS);
                }
            }            如果(sliderTimerStop!= NULL)
                sliderTimerStop.setText(stringForTime(持续时间));
            如果(sliderTimerStart!= NULL)
                sliderTimerStart.setText(stringForTime(位置));
        }
    });    返回的位置;

}

To update a seekbar, I am using the following code: My problem is that anytime the seekBar.setProgress() is call, other element on the UI become freezed, so I would like to have a different thread that update the seekBar in the main thread.

How to proceed ?

    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;
}

解决方案

Activities have a runOnUiThread method that allows separate threads to update UI components. Your setProgress method would end up looking like:

private int setProgress() {

    if (mBoundService == null || mDragging) {
        return 0;
    }
    final int position = mBoundService.getCurrentPosition();
    final int duration = mBoundService.getDuration();

    runOnUiThread(new Runnable(){

        @Override
        public void run(){

            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;

}

这篇关于安卓:从线程更新界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆