安卓多線程編程系列7:使用Looper的不同方式(子線程中實例化Handler方式)

在Activity主線程中實例化Handler,不需要定義Looper,因爲主線程中有一個默認的Looper對象。而在子線程中實例化Handler,必須要定義Looper循環消息隊列和消息隊列循環結束。下面我們來一起看一下在子線程中實例化Handler'方式。

整體思路:在xml文件中放置一個Button控件和一個TextView控件,在activity中定義一個MyThread類實現Runnable接口,在這個類的run方法中控制Looper的循環和停止,並實例化Handler,在Handler類的handleMessage方法中接收傳遞消息的數據,並在onCreate方法中定義Message,賦值數據,並使用handler發送。

activity_main.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=".MainActivity" >



    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="116dp"
        android:text="發送消息" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp"
       />

</RelativeLayout>
MainActivity.java文件:

package com.example.android_handler_looper;
//
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
//在Activity主線程中有一個默認的Looper對象,來處理子線程中發送的消息;而在子線程中必須定義Looper
public class MainActivity extends Activity {

	private Button button;
	private TextView textView; 
	private Handler handler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button=(Button)findViewById(R.id.button1);
		textView=(TextView)findViewById(R.id.textView1);
//		啓動線程
		new Thread(new MyThread()).start();
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Message message=Message.obtain();
				message.obj="Rose";
				handler.sendMessage(message);
			}
		});
	}

	public class MyThread implements Runnable{

		@Override
		public void run() {
			
			Looper.prepare();//循環消息隊列
			handler=new Handler(){
				@Override
				public void handleMessage(Message msg) {
					// TODO Auto-generated method stub
					super.handleMessage(msg);
//					不能在子線程中更新UI
//					textView.setText("從UI主線程中獲取消息:"+msg.obj);
					Toast.makeText(MainActivity.this, String.valueOf(msg.obj), 1).show();
				}
			};
			Looper.loop();//直到消息隊列循環結束
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章