当前位置: 移动技术网 > 移动技术>移动开发>Android > android使用多线程更新ui示例分享

android使用多线程更新ui示例分享

2019年07月24日  | 移动技术网移动技术  | 我要评论

android线程涉及的技术有:handler;message;messagequeue;looper;handlerthread。

下面看一段在线程中更新ui的代码:

复制代码 代码如下:

public class mainactivity extends activity {
private textview timelable;
private button stopbtn;
private thread mthread;
private boolean isrunning = true;
private int timecount = 0;

@override
protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    timelable = (textview) findviewbyid(r.id.timelable);
    stopbtn = (button) findviewbyid(r.id.stop);
    stopbtn.setonclicklistener(new onclicklistener() {

        @override
        public void onclick(view v) {              
            isrunning = false;
        }
    });

    mthread = new thread(new runnable() {

        @override
        public void run() {
            while (isrunning) {
                try {
                    thread.sleep(1000);
                    timecount++;
                    timelable.settext("timecount=" + timecount + " 秒");
                } catch (exception e) {
                    e.printstacktrace();
                }
            }

        }
    });
    mthread.start();       
}
}

这段代码只是在线程中更新textview的显示内容,但是执行后看不到效果,并且报了一个错:android.view.viewrootimpl$calledfromwrongthreadexception: only the original thread that created a view hierarchy can touch its views.
在android中更新ui处理必须由创建它的线程更新,而不能在其他线程中更新。上面的错误原因就在于此。

由于timelable是一个ui控件,它是在主线程中创建的,但是它却在子线程中被更新了,更新操作在mthread线程的run()方法中实现。这样的处理违背了android多线程编程规则,系统会抛出异常。

要解决这个问题,就要明确主线程和子线程的职责。主线程的职责是创建、显示和更新ui控件、处理ui事件、启动子线程、停止子线程等;子线程的职责是计算时间和向主线程发出更新ui消息,而不是直接更新ui。子线程向主线程发送消息可以用handler实现。代码如下:

复制代码 代码如下:

public class mainactivity extends activity {

private textview timelable;
private button stopbtn;
private thread mthread;
private boolean isrunning = true;
private int timecount = 0;

final private handler mhandler = new handler(){
    public void handlemessage(message msg) {
        switch (msg.what) {
        case 0 :
            timelable.settext("timecount=" + timecount + " 秒");
            break;
        default :
            break;
        }
    }
};

@override
protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    timelable = (textview) findviewbyid(r.id.timelable);
    stopbtn = (button) findviewbyid(r.id.stop);
    stopbtn.setonclicklistener(new onclicklistener() {

        @override
        public void onclick(view v) {

            isrunning = false;
        }
    });

    mthread = new thread(new runnable() {

        @override
        public void run() {
            while (isrunning) {
                try {
                    thread.sleep(1000);
                    timecount++;
                    mhandler.sendemptymessage(0);
                } catch (exception e) {
                    e.printstacktrace();
                }
            }

        }
    });
    mthread.start();
}
}


运行后不会报之前的错,textview也能正常更新内容了。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网