Android 主线程和线程之间相互发送消息

为君一笑 提交于 2019-12-15 17:39:26

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

    通过分析Activity源码,我们知道每个Activity都有一个Looper,所以主线程在接收Message是不需要调用Looper.prepare()和Looper.loop(),但是线程是不带Looper的,当线程要接收来自主线程的消息是就需要调用Looper.prepare()和Looper.loop()。以下我们将通过一个示例,讲述主线程和线程之间如何发送Message。

 

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

/**
 * 这个类是主线程,它每隔两秒就发送一个Message给线程,线程收到Message后将发送一个Message给主线程,主线程收到Message后会将Message内容Toast出来。
 * org.fneg.HandlerTestActivity.java
 * Create at: 2012-6-4 下午9:39:19
 * @author:feng
 * Email:fengcunhan@gmail.com
 *
 */
public class HandlerTestActivity extends Activity {
    private Handler uiHandler;
    private ThreadWithLooper thread;
    private Runnable showRunable;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        uiHandler=new Handler(){
        	@Override
        	public void handleMessage(Message msg) {
        		switch(msg.what){
				case Messages.MSG_HELLO:
					 Toast.makeText(HandlerTestActivity.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
					break;
					
				}
        	}
        };
        thread=new ThreadWithLooper(uiHandler);
		
		thread.start();
        showRunable=new Runnable() {
			
			@Override
			public void run() {
				//給线程发送一个Message
				thread.getHandler().sendEmptyMessage(Messages.MSG_HELLO);
				uiHandler.postDelayed(this, 2*1000);
			}
		};
        uiHandler.post(showRunable);
        
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	uiHandler.removeCallbacks(showRunable);
    }
}

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

/**
 * 从线程发送消息到UI线程(主线程)
 * org.fneg.ThreadWithLooper.java
 * Create at: 2012-6-4 下午4:58:11
 * @author:feng<br/>
 * Email:fengcunhan@gmail.com
 *
 */
public class ThreadWithLooper extends Thread {
	private Handler handler;
	
	private Handler uiHandler;
	
	public ThreadWithLooper(Handler mHandler){
		this.uiHandler=mHandler;
		//初始化Handler,接收到主线程发送过来的Message就回复一个Message给主线程,消息内容是 一个字符串和当前时间
		handler =new Handler(){

			@Override
			public void handleMessage(Message msg) {
				switch(msg.what){
				case Messages.MSG_HELLO:
					 Message message=new Message();
					 message.what=Messages.MSG_HELLO;
					 message.obj="Yes!I get a hello"+System.currentTimeMillis();
					 uiHandler.sendMessage(message);
					break;
					
				}
			}
			
		};
	}
	
	public Handler getHandler() {
		return handler;
	}

	public void setHandler(Handler handler) {
		this.handler = handler;
	}
	
	@Override
	public void run() {
		Looper.prepare();
		
		Looper.loop();
	}
	
	
}

/**
 *
 * org.fneg.Messages.java
 * Create at: 2012-6-4 下午4:51:20
 * @author:feng
 * Email:fengcunhan@gmail.com
 *
 */
public class Messages {
   public static final int MSG_HELLO=0X1;
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!