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


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

    01 import android.app.Activity;
    02 import android.os.Bundle;
    03 import android.os.Handler;
    04 import android.os.Message;
    05 import android.widget.Toast;
    06  
    07 /**
    08  * 这个类是主线程,它每隔两秒就发送一个Message给线程,线程收到Message后将发送一个Message给主线程,主线程收到Message后会将Message内容Toast出来。
    09  * org.fneg.HandlerTestActivity.java
    10  * Create at: 2012-6-4 下午9:39:19
    11  * @author:feng
    12  * Email:fengcunhan@gmail.com
    13  *
    14  */
    15 public class HandlerTestActivity extends Activity {
    16     private Handler uiHandler;
    17     private ThreadWithLooper thread;
    18     private Runnable showRunable;
    19     @Override
    20     public void onCreate(Bundle savedInstanceState) {
    21         super.onCreate(savedInstanceState);
    22         setContentView(R.layout.main);
    23         uiHandler=new Handler(){
    24             @Override
    25             public void handleMessage(Message msg) {
    26                 switch(msg.what){
    27                 case Messages.MSG_HELLO:
    28                      Toast.makeText(HandlerTestActivity.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
    29                     break;
    30                      
    31                 }
    32             }
    33         };
    34         thread=new ThreadWithLooper(uiHandler);
    35          
    36         thread.start();
    37         showRunable=new Runnable() {
    38              
    39             @Override
    40             public void run() {
    41                 //給线程发送一个Message
    42                 thread.getHandler().sendEmptyMessage(Messages.MSG_HELLO);
    43                 uiHandler.postDelayed(this, 2*1000);
    44             }
    45         };
    46         uiHandler.post(showRunable);
    47          
    48     }
    49      
    50     @Override
    51     protected void onStop() {
    52         super.onStop();
    53         uiHandler.removeCallbacks(showRunable);
    54     }
    55 }
    01 import android.os.Handler;
    02 import android.os.Looper;
    03 import android.os.Message;
    04  
    05 /**
    06  * 从线程发送消息到UI线程(主线程)
    07  * org.fneg.ThreadWithLooper.java
    08  * Create at: 2012-6-4 下午4:58:11
    09  * @author:feng<br/>
    10  * Email:fengcunhan@gmail.com
    11  *
    12  */
    13 public class ThreadWithLooper extends Thread {
    14     private Handler handler;
    15      
    16     private Handler uiHandler;
    17      
    18     public ThreadWithLooper(Handler mHandler){
    19         this.uiHandler=mHandler;
    20         //初始化Handler,接收到主线程发送过来的Message就回复一个Message给主线程,消息内容是 一个字符串和当前时间
    21         handler =new Handler(){
    22  
    23             @Override
    24             public void handleMessage(Message msg) {
    25                 switch(msg.what){
    26                 case Messages.MSG_HELLO:
    27                      Message message=new Message();
    28                      message.what=Messages.MSG_HELLO;
    29                      message.obj="Yes!I get a hello"+System.currentTimeMillis();
    30                      uiHandler.sendMessage(message);
    31                     break;
    32                      
    33                 }
    34             }
    35              
    36         };
    37     }
    38      
    39     public Handler getHandler() {
    40         return handler;
    41     }
    42  
    43     public void setHandler(Handler handler) {
    44         this.handler = handler;
    45     }
    46      
    47     @Override
    48     public void run() {
    49         Looper.prepare();
    50          
    51         Looper.loop();
    52     }
    53      
    54      
    55 }
    01 /**
    02  *
    03  * org.fneg.Messages.java
    04  * Create at: 2012-6-4 下午4:51:20
    05  * @author:feng
    06  * Email:fengcunhan@gmail.com
    07  *
    08  */
    09 public class Messages {
    10    public static final int MSG_HELLO=0X1;
    11 }


  • 相关阅读:
    网页效果图设计之色彩索引
    支持树型的GridView控件
    (转)CSS布局参考 IE的If条件注释使用备忘
    firefox与IE对javascript和CSS的区别
    转 CSS半透明滤镜在FIREFOX和IE下面的能够通用的写法
    無法為 SSL/TLS 安全通道建立信任關係,忽略憑証檢查的方式
    临时
    EventUtil
    js测试对象包含特性
    重学概率论的一点点理解(持续更新)
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3212309.html
Copyright © 2020-2023  润新知