(自) Activity,Service属于主线程,在主线程中才能更新UI,如toast等。其他线程中不能直接使用,这时可以使用Handler来处理,Handler可以在Activity和Service中。
关于在非UI线程中进行UI操作会出现问题: Can’t create handler inside thread that has not called Looper.prepare()
这时有两种方式来解决:
(一)在该非UI线程中创建消息队列(因为创建的工作线程默认是没有消息循环和消息队列的),Looper.prepare();…..;Looper.loop();
newThread() {
public void run() {
Looper.prepare(); //创建消息队列
todo();
Looper.loop();//进入消息循环
}}.start();
(二)运用Handler机制:
package com.simon;
import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log; …