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

转自:http://blog.csdn.net/jianghuiquan/article/details/8641071

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

一、设计界面

  1、布局文件

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

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/btnmain"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="自主线程发送消息给自己" />  
  13.   
  14.     <Button  
  15.         android:id="@+id/btnother"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="自其它线程发送消息至主线程" />  
  19.       
  20.         <TextView  
  21.         android:id="@+id/tvmsg"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="" />  
  25.       
  26.   
  27. </LinearLayout>  


二、程序文件

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

[java] view plaincopy
  1. package com.genwoxue.handlemessageloop;  
  2.   
  3. import android.app.Activity;    
  4. import android.os.Bundle;    
  5. import android.os.Handler;    
  6. import android.os.Looper;    
  7. import android.os.Message;    
  8. import android.util.Log;    
  9. import android.view.View;    
  10. import android.view.View.OnClickListener;    
  11. import android.widget.Button;    
  12. import android.widget.TextView;    
  13.   
  14. public class MainActivity extends Activity{    
  15.             
  16.         private static long s=0;  
  17.         private String TAG = "HandlerTest";    
  18.         private ReceiveMessageThread receiveMessageThread =null;    
  19.         private EventHandler mHandler = null;     
  20.             
  21.         private Button btnMain = null;    
  22.         private Button btnOther = null;    
  23.         private TextView tvMsg = null;   
  24.           
  25.         @Override    
  26.         public void onCreate(Bundle savedInstanceState) {    
  27.             super.onCreate(savedInstanceState);    
  28.             setContentView(R.layout.activity_main);    
  29.               
  30.             btnMain = (Button)super.findViewById(R.id.btnmain);    
  31.             btnOther = (Button)super.findViewById(R.id.btnother);    
  32.             tvMsg = (TextView)super.findViewById(R.id.tvmsg);    
  33.               
  34.             btnMain.setOnClickListener(new OnClickListener(){  
  35.                 @Override   
  36.                 public void onClick(View v){  
  37.                      //主线程发送消息给自己     
  38.                     Looper looper = Looper.myLooper();//get the Main looper related with the main thread      
  39.                     //如果不给任何参数的话会用当前线程对应的Looper(这里就是Main Looper)为Handler里面的成员mLooper赋值     
  40.                     mHandler = new EventHandler(looper);    
  41.                     // 清除整个MessageQueue里的消息       
  42.                     mHandler.removeMessages(0);    
  43.                     String obj = "This main thread's message and received by itself!";    
  44.                         
  45.                     Message msg = mHandler.obtainMessage(1,1,1,obj);    
  46.                     // 将Message对象送入到main thread的MessageQueue里面      
  47.                     mHandler.sendMessage(msg);    
  48.                 }  
  49.             });  
  50.   
  51.             btnOther.setOnClickListener(new OnClickListener(){  
  52.                   
  53.                 @Override   
  54.                 public void onClick(View v){  
  55.                      //other线程发送消息给主线程     
  56.                     receiveMessageThread = new ReceiveMessageThread();      
  57.                     receiveMessageThread.start();      
  58.                 }  
  59.             });  
  60.           
  61.         }    
  62.             
  63.         class EventHandler extends Handler{    
  64.                 
  65.             public EventHandler(Looper looper){    
  66.                 super(looper);    
  67.             }    
  68.                 
  69.             public EventHandler(){    
  70.                 super();    
  71.             }    
  72.             @Override    
  73.             public void handleMessage(Message msg) {    
  74.                 super.handleMessage(msg);    
  75.                 Log.e(TAG, "CurrentThread id:----------+>" + Thread.currentThread().getId());    
  76.                 switch(msg.what){    
  77.                 case 1:    
  78.                     tvMsg.setText((String)msg.obj);    
  79.                     break;    
  80.                 default:    
  81.                     Log.e(TAG,(String)msg.obj);    
  82.                     break;    
  83.                 }    
  84.             }    
  85.                 
  86.         }    
  87.             
  88.         //ReceiveMessageThread has his own message queue by execute Looper.prepare();      
  89.         class ReceiveMessageThread extends Thread {    
  90.                 
  91.             @Override    
  92.             public void run(){    
  93.                 Looper.prepare();    
  94.                     
  95.                     Looper looper = Looper.getMainLooper();      
  96.                     mHandler = new EventHandler(looper);    
  97.                     mHandler.removeMessages(0);    
  98.                       
  99.                     s=s+1;  
  100.                     Message msg =new Message();  
  101.                     msg.what=1;  
  102.                     msg.obj= String.valueOf(s);    
  103.                     // 将Message对象送入到main thread的MessageQueue里面      
  104.                     mHandler.sendMessage(msg);   
  105.                     try {  
  106.                         Thread.sleep(1000);  
  107.                     } catch (InterruptedException e) {  
  108.                         e.printStackTrace();  
  109.                     }  
  110.                       
  111.                  Looper.loop();    
  112.             }    
  113.         }    
  114.             
  115. }    


三、配置文件

  打开“AndroidManifest.xml”文件。

  然后输入以下代码:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.genwoxue.handlemessageloop"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="10"  
  9.         android:targetSdkVersion="15" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.genwoxue.handlemessageloop.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.     </application>  
  25.   
  26. </manifest>  

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

四、运行结果

   


原文地址:https://www.cnblogs.com/walccott/p/4957604.html