第80章、Handle-Message-Looper消息机制之一(从零开始学Android)

  本章着重了解一下Handle、Message、Looper用法。

一、设计界面

  1、布局文件

  打开res/layout/activity_main.xml文件。
  输入以下代码:




    


二、程序文件

  打开“src/com.genwoxue.handlemessageloop/MainActivity.java”文件。
  然后输入以下代码:

package com.genwoxue.handlemessageloop;

import android.app.Activity;  
import android.os.Bundle;  
import android.os.Handler;  
import android.os.Looper;  
import android.os.Message;  
import android.util.Log;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  

public class MainActivity extends Activity{  
	      
		private static long s=0;
	    private String TAG = "HandlerTest";  
	    private ReceiveMessageThread receiveMessageThread =null;  
	    private EventHandler mHandler = null;   
	      
	    private Button btnMain = null;  
	    private Button btnOther = null;  
	    private TextView tvMsg = null; 
	    
	    @Override  
	    public void onCreate(Bundle savedInstanceState) {  
	        super.onCreate(savedInstanceState);  
	        setContentView(R.layout.activity_main);  
	        
	        btnMain = (Button)super.findViewById(R.id.btnmain);  
		    btnOther = (Button)super.findViewById(R.id.btnother);  
		    tvMsg = (TextView)super.findViewById(R.id.tvmsg);  
	        
		    btnMain.setOnClickListener(new OnClickListener(){
	        	@Override 
	        	public void onClick(View v){
	        		 //主线程发送消息给自己   
		            Looper looper = Looper.myLooper();//get the Main looper related with the main thread    
		            //如果不给任何参数的话会用当前线程对应的Looper(这里就是Main Looper)为Handler里面的成员mLooper赋值   
		            mHandler = new EventHandler(looper);  
		            // 清除整个MessageQueue里的消息     
		            mHandler.removeMessages(0);  
		            String obj = "This main thread's message and received by itself!";  
		              
		            Message msg = mHandler.obtainMessage(1,1,1,obj);  
		            // 将Message对象送入到main thread的MessageQueue里面    
		            mHandler.sendMessage(msg);  
	        	}
	        });

	        btnOther.setOnClickListener(new OnClickListener(){
	        	
	        	@Override 
	        	public void onClick(View v){
	        		 //other线程发送消息给主线程   
		            receiveMessageThread = new ReceiveMessageThread();    
		            receiveMessageThread.start();    
	        	}
	        });
	    
	    }  
	      
	    class EventHandler extends Handler{  
	          
	        public EventHandler(Looper looper){  
	            super(looper);  
	        }  
	          
	        public EventHandler(){  
	            super();  
	        }  
	        @Override  
	        public void handleMessage(Message msg) {  
	            super.handleMessage(msg);  
	            Log.e(TAG, "CurrentThread id:----------+>" + Thread.currentThread().getId());  
	            switch(msg.what){  
	            case 1:  
	            	tvMsg.setText((String)msg.obj);  
	                break;  
	         	default:  
	                Log.e(TAG,(String)msg.obj);  
	                break;  
	            }  
	        }  
	          
	    }  
	      
	    //ReceiveMessageThread has his own message queue by execute Looper.prepare();    
	    class ReceiveMessageThread extends Thread {  
	          
	        @Override  
	        public void run(){  
	            Looper.prepare();  
		          
	            	Looper looper = Looper.getMainLooper();    
		            mHandler = new EventHandler(looper);  
		            mHandler.removeMessages(0);  
		            
		            s=s+1;
		            Message msg =new Message();
		            msg.what=1;
		            msg.obj= String.valueOf(s);  
		            // 将Message对象送入到main thread的MessageQueue里面    
		            mHandler.sendMessage(msg); 
		            try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
		            
	             Looper.loop();  
	        }  
	    }  
	      
}  


三、配置文件

  打开“AndroidManifest.xml”文件。

  然后输入以下代码:




    

    
        
            
                
                
            
        
    

  注意:采用默认AndroidManifest.xml即可,无需额外配置。

四、运行结果

   

你可能感兴趣的:(Android,从零开始学Android)