Android线程涉及的技术有:Handler;Message;MessageQueue;Looper;HandlerThread。
下面看一段在线程中更新UI的代码:
@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实现。代码如下:
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();
}
}