• Android IPC机制之Messenger


    Messenger:两个进程通过Messenger传递消息,进程1和进程2中都需要创建一个Messenger,创建过程:首先进程2需要创建一个服务,

    并在服务中创建一个Messenger对象,进程1通过绑定进程2的服务,在绑定服务时进程2通过Messenger返回一个IBinder对象,

    进程1通过绑定服务后返回的IBinder对象,创建一个进程1的Messenger对象这样就和进程2建立联系,就可以通过Messenger进行消息传递


    服务端:
    1、在服务中创建一个Messenger,用于向客户端发送消息;
    2、服务端服务在被绑定时通过Messenger获取IBinder对象并返回,这时就和客户端建立了联系。
    3、客户端发送消息后,在服务端的Handler回调中的,可通过msg.reply方法获取客户端传递过来的messenger,

    然后创建messge,加入messenger中,并由messenger传递给客户端


    客户端:
    1、首先绑定服务端的服务,服务端通过创建Messenger,从Messenger中获取客户端需要的IBinder对象并返回,

    客户端根据服务端返回的IBinder对象,创建客户端的Messenger对象,通过Messenger和服务端建立联系,并进行消息传递;
    2、客户端创建一个Message,由Messenger携带并传入服务端

    示例代码如下:

    首先是服务端代码:

     1 package com.jiao.myaidl;
     2 
     3 import android.app.Service;
     4 import android.content.Intent;
     5 import android.os.Bundle;
     6 import android.os.Handler;
     7 import android.os.IBinder;
     8 import android.os.Message;
     9 import android.os.Messenger;
    10 import android.os.RemoteException;
    11 
    12 
    13 public class MessengerService extends Service {
    14     public MessengerService() {
    15     }
    16 
    17     //创建Messenger对象
    18     private final Messenger mMessenger = new Messenger(new ServiceHandler());
    19 
    20     //消息回调,处理客户端传递过来的message对象
    21     private class ServiceHandler extends Handler {
    22 
    23         @Override
    24         public void handleMessage(Message msg) {
    25             super.handleMessage(msg);
    26 
    27             switch (msg.what) {
    28                 case 0:
    29 
    30                     System.out.println("客户端发过来的:" + msg.getData().getString("client"));
    31                     Messenger clientMessenger = msg.replyTo;//获取客户端传过来的Messenger对象
    32                     Message replyMessage = Message.obtain();//创建一个返回的message对象
    33                     replyMessage.what = 1;
    34                     Bundle bundle = new Bundle();
    35                     bundle.putString("server_reply", "i come from server");
    36                     replyMessage.setData(bundle);
    37 
    38                     try {
    39                         //通过客户端传过来的Messenger 将返回数据带回去
    40                         clientMessenger.send(replyMessage);
    41                     } catch (RemoteException e) {
    42                         e.printStackTrace();
    43                     }
    44                     break;
    45             }
    46         }
    47     }
    48 
    49     ;
    50 
    51 
    52     @Override
    53     public IBinder onBind(Intent intent) {
    54         //通过Messenger获取IBinder对象并返回
    55         return mMessenger.getBinder();
    56     }
    57 
    58 }

    在清单文件中注册服务:

    1    <service android:name="com.jiao.myaidl.MessengerService">
    2             <intent-filter>
    3                 <action android:name="com.jiao.myaidl.action.MESSENGER_SERVICE" />
    4                 <category android:name="android.intent.category.DEFAULT" />
    5             </intent-filter>
    6         </service>

    客户端代码:

     1 package com.jiao.myaidl;
     2 
     3 import android.content.ComponentName;
     4 import android.content.Context;
     5 import android.content.Intent;
     6 import android.content.ServiceConnection;
     7 import android.os.Bundle;
     8 import android.os.Handler;
     9 import android.os.IBinder;
    10 import android.os.Message;
    11 import android.os.Messenger;
    12 import android.os.RemoteException;
    13 import android.support.v7.app.AppCompatActivity;
    14 import android.view.View;
    15 import android.widget.Button;
    16 
    17 import com.jiao.myaidl.client.R;
    18 
    19 
    20 public class MessengerActivity extends AppCompatActivity {
    21 
    22     private Messenger mMessenger;
    23 
    24     private class clientHandler extends Handler {
    25 
    26         @Override
    27         public void handleMessage(Message msg) {
    28             super.handleMessage(msg);
    29             switch (msg.what) {
    30 
    31                 case 1:
    32                     String server_reply = msg.getData().getString("server_reply");
    33                     System.out.println("服务器返回:" + server_reply);
    34                     break;
    35             }
    36         }
    37     }
    38 
    39     private ServiceConnection mConnection = new ServiceConnection() {
    40         @Override
    41         public void onServiceConnected(ComponentName name, IBinder service) {
    42             mMessenger = new Messenger(service);
    43             System.out.println("绑定服务成功");
    44         }
    45 
    46         @Override
    47         public void onServiceDisconnected(ComponentName name) {
    48 
    49             bindService();
    50         }
    51     };
    52 
    53     @Override
    54     protected void onCreate(Bundle savedInstanceState) {
    55         super.onCreate(savedInstanceState);
    56         setContentView(R.layout.activity_messenger);
    57 
    58 
    59         bindService();
    60 
    61         Button bt = (Button) findViewById(R.id.bt_messenger);
    62         bt.setOnClickListener(new View.OnClickListener() {
    63             @Override
    64             public void onClick(View v) {
    65                 sendToService();
    66             }
    67         });
    68     }
    69 
    70 
    71     private void bindService() {
    72 
    73         Intent it = new Intent();
    74         it.setAction("com.jiao.myaidl.action.MESSENGER_SERVICE");
    75         it.setPackage("com.jiao.myaidl.server");
    76         bindService(it, mConnection, Context.BIND_AUTO_CREATE);
    77     }
    78 
    79 
    80     private void sendToService() {
    81         Message messageClient = Message.obtain(null, 0);
    82         //用于传递给服务端回复的Messenger
    83         Messenger replyMessenger = new Messenger(new clientHandler());
    84         Bundle bundle = new Bundle();
    85         bundle.putString("client", "i come from client");
    86         messageClient.setData(bundle);
    87         //通过Message的replyTo属性将Messenger对象传递到服务端
    88         messageClient.replyTo = replyMessenger;
    89         try {
    90             mMessenger.send(messageClient);
    91         } catch (RemoteException e) {
    92             e.printStackTrace();
    93         }
    94     }
    95 
    96 
    97 }

    以上就是两个进程之间通过Messenger实现消息通信;

  • 相关阅读:
    Eclipse
    Android View Attributes
    Android Virtual Device
    Bootstrap
    Nginx常用命令
    [多线程]多线程(Thread、Runnable、Callable)
    Redis、MongoDB及Memcached的区别
    Linux将某目录授权给某组里的某用户
    CentOS修改locale解决调用API乱码问题
    MyBatis与Hibernate的区别?
  • 原文地址:https://www.cnblogs.com/all88/p/5274752.html
Copyright © 2020-2023  润新知