Android 消息機制 進程調度問題
“子線程不能更新主線程各個組件的狀態。”
下面通過一個自動增長的操作講解這一點:
定義佈局管理器main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
我們首先定義這樣Activity程序,通過Timer類完成定時更新:
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
//用來更新文本信息
public class MessageDemo extends Activity {
private TextView info = null;
private static int count = 0; //表示更新後的記錄
private static final int SET = 1; //操作的what狀態
private Handler myHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
info = (TextView) super.findViewById(R.id.info);
Timer timer = new Timer();
timer.schedule(new MyTask(), 0, 1000);
}
private class MyTask extends TimerTask{
@Override
public void run() {
MessageDemo.this.info.setText("消息處理"+ count++); //我們在子線程更新主線程中的count
}
}
}
運行後發現報錯。
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
所以我們只能在主線程中更新自身的操作,通過子線程的變化,讓主線程修改自身的count++操作。
修改後的Activity程序:
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
//用來更新文本信息
public class MessageDemo extends Activity {
private TextView info = null;
private static int count = 0; //表示更新後的記錄
private static final int SET = 1; //操作的what狀態
private Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case SET:
MessageDemo.this.info.setText("消息處理"+ count++);
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
info = (TextView) super.findViewById(R.id.info);
Timer timer = new Timer();
timer.schedule(new MyTask(), 0, 1000);
}
private class MyTask extends TimerTask{
@Override
public void run() {
Message msg = new Message();
msg.what = SET;
MessageDemo.this.myHandler.sendMessage(msg);
}
}
}
運行後可以程序可以正常運行。