使用線程更改Ui
在Android 中,UI 是非線程安全的,如果使用一個線程修改一個TextView
的Text
的話,程序則會崩潰。
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.met0.android.sms.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_centerVertical="true"
android:text="TextView" />
</RelativeLayout>
這是一個RelativeLayout
佈局文件,裏面有一個Button
和TextView
。
MainActivity.java
public class MainActivity extends Activity implements OnClickListener,Runnable {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView1);
}
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
textView.setText("update text success");
}
}).start();
}
onClick(View v)
當點擊了界面的按鈕時,則會在這個方法啓動線程,修改textView1 對象的 text。
運行一下
運行程序,點擊按鈕啓動線程修改textView1
對象的Text
則會出現這個情況。
使用Handler 線程修改
修改代碼
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{
private Button button;
private TextView textView;
private static final int UPDATE_TEXT = 1;
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_TEXT:
textView.setText("update editView success!!");
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView1);
}
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
Message msg = new Message();
msg.what = UPDATE_TEXT;
handler.sendMessage(msg);
}
}).start();;
}
}
先看onClick(View v)
方法
Message msg = new Message();
創建了一個對象,這個對象用來存放一些數據,msg.what = UPDATE_TEXT;
就算其中一種存放方式。
handler.sendMessage(msg);
在創建handler
的時候,重寫了它的handlerMessage
方法,而在handlerMessage
方法中修改了textView1
的 text
,這樣就解決了線程Ui 修改造成的程序崩潰。
運行一下
Hanlder 機制
Message
在線程中起到了非常重要的作用,它是用來傳遞數據。這些數據可以告訴Hanlder
如何操作。
Hanlder
是用來發送消息SendMessage(Message msg)
和處理消息HandlerMessage(Message msg)
。
MessageQueue
消息隊列,在Hanlder 中。
Looper
在消息隊列中獲取Message
,一直運行着,一旦MessageQueue
中有Message
則會調用HandlerMessage(Message msg)
進行處理。