一.概念

    实现多线程之间的信息传递的机制。

二.用法

   1.Message  消息

      1-创建 Message.obtain(),使用消息池获取或创建新消息。

      2-public 属性

         1>what int  表示id

         2>arg1  int  参数1

         3>arg2  int  参数2

         4>obj   Object  对象

   2.Handler  处理器

      1-Message的处理器,也负责发送和移除

      2-方法

        1>sendMessage(Message)  发送即时消息,即时发送和处理

        2>sendMessageDelayed(Message,延时时间ms数)  发送延时消息

        3>sendEmptyMessage(int what)  发送空消息

        4>sendEmptyMessageDelayed(int what,延时时间ms数)  发送延时空消息

        5>handlerMessage(Message msg)  处理消息的回调方法

        6>移除还未处理的消息 removeMessage(int what)

   3.MessageQueue   消息队列

   4.Looper  循环器

 

消息的即时与延时接收:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.wang.testapp2.TestActivity6"
11     android:orientation="vertical">
12 
13 
14     <TextView
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:text="接收消息"
18         android:id="@+id/tv_5"/>
19 
20     <Button
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:text="发消息"
24         android:onClick="bt2_OnClick"/>
25 
26    
27 
28 </LinearLayout>
.xml

 

 1 package com.example.wang.testapp2;
 2 
 3 import android.os.Handler;
 4 import android.os.Message;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.TextView;
 9 import android.widget.Toast;
10 import java.util.Random;
11 
12 
13 public class TestActivity6 extends AppCompatActivity {
14 
15     TextView tv_3,tv_4,tv_5,tv_6;
16 
17     //定义Handler
18 
19     Handler h=new Handler(){
20 
21         @Override
22         public void handleMessage(Message msg) {
23             super.handleMessage(msg);
24 
25             //处理消息
26             if (msg.what==1)
27             {
28                 String m=msg.obj.toString();
29 
30                 tv_5.setText(tv_5.getText()+" "+m);
31             }
32             else if(msg.what==2)
33             {
34                 tv_5.setText(tv_5.getText()+" 空消息");
35             }
36         }
37     };
38 
39     @Override
40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         setContentView(R.layout.activity_test6);
43 
44         tv_5=(TextView)findViewById(R.id.tv_5);
45 
46     }
47 
48 
49 
50     //发消息
51     public  void bt2_OnClick(View v)
52     {
53         //启动线程
54         new  Thread(){
55             @Override
56             public void run() {
57 
58                 //发送消息
59                 //1.创建Message
60 
61                 Message m=Message.obtain();
62                 m.what=1;//id
63                 m.obj="新信息1";
64 
65                 //2.发送即时消息
66                 h.sendMessage(m);
67 
68                 m=Message.obtain();
69                 m.what=1;//id
70                 m.obj="新信息2";
71 
72                 //发送延时消息
73                 h.sendMessageDelayed(m, 2000);
74 
75                 h.sendEmptyMessage(2);
76 
77                 h.sendEmptyMessageDelayed(2,3000);
78 
79 
80             }
81         }.start();
82     }
83 
84 }
.java

 

 

posted on 2016-05-17 00:04  安然罒  阅读(254)  评论(0编辑  收藏  举报