通过Handler实现线程间通信

1、在WorkerThread当中通过Handler发送消息,在MainThread当中实现Handler的handleMessage()方法

package com.test.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

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);
		textView = (TextView) findViewById(R.id.textViewId);
		button = (Button) findViewById(R.id.buttonId);
		button.setOnClickListener(new buttonListener());
		handler = new myHandler();
	}

	class myHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			System.out.println("handleMessage的线程是:"+ Thread.currentThread().getName());
			textView.setText(msg.obj.toString());
		}
	}

	class myThread extends Thread {
		@Override
		public void run() {
			System.out.println("myThread的线程是:"+ Thread.currentThread().getName());
			try {
				Thread.sleep(2 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			String s = "从WorkerThread获取到的数据";
			Message message = handler.obtainMessage();
			message.obj = s;
			handler.sendMessage(message);
		}
	}

	class buttonListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Thread t = new myThread();
			t.start();
		}
	}
}

通过Handler实现线程间通信_第1张图片

sendMessage()方法不论是在MainThread当中发送还是在WorkerThread发送,都是可以的

 

2、在MainThread当中通过Handler发送消息,在WorkerThread当中实现Handler的handleMessage()方法

①准备Looper对象

②在WorkerThread中生成handler对象

③在MainThread中发送消息

package com.test.handler_02;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private Button button;
	private Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) findViewById(R.id.buttonId);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				System.out.println("监听器的线程是:"+ Thread.currentThread().getName());
				Message message = handler.obtainMessage();
				handler.sendMessage(message);
			}
		});
		workerThread wt = new workerThread();
		wt.start();
	}

	class workerThread extends Thread {

		@Override
		public void run() {
			// 准备Looper对象
			Looper.prepare();
			// 生成Handler对象
			handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					System.out.println("workerThread的线程是:"+ Thread.currentThread().getName());
					System.out.println("收到了消息");
				}
			};
			// 调用loop方法取出消息对象,再调用handler的handleMessage方法处理
			Looper.loop();
		}
	}
}

通过Handler实现线程间通信_第2张图片

你可能感兴趣的:(handler)