Android中进程间的通信之Messenger

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Jack_Li_ng/article/details/52691144

Android中进程间的通信之Messenger

谈到android中的IPC机制,很多人都会先想到AIDL,这里要讲的是Messenger ,利用Messenger 可以简单的实现进程间的通信。通过发送消息到handler,它是基于消息的跨进程通信。
1、先写一个服务端MessageService,并且在新开启一个进程,在onBind()中通过Hmessage对象获取binder.

package com.ricky.messagedemo;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Messenger;
import android.os.Message;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

public class MessageService extends Service {
    private Messenger Hmessage=new Messenger(new MessagerHandler());


    class MessagerHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
                switch (msg.what){
                    case 10:
                        Log.e("service",msg.getData().getString("message")); //打印客户端发送的消息
                        Messenger client=msg.replyTo;
                        Message message=Message.obtain(null,11);
                        Bundle mBundle=new Bundle();
                        mBundle.putString("client","服务端已接受到消息");
                        message.setData(mBundle);
                        try {
                            client.send(message);  //通知客户端已接收到消息
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                        break;
                    default:
                        break;

                }

        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return Hmessage.getBinder();
    }



}
在AndroidManifest.xml注册服务,新开启进程为remote1
<service
            android:name=".MessageService"
            android:process=":remote1"></service>

2、在Activity中绑定服务并向服务端发送消息,同时,在Activity也要写一个handler,接收服务返回过来的消息。服务端向客户端返回消息的关键replyTo,用replyTo可以在服务端接收到来自客户端的Messenger。再利用
Messenger对象给客户端返回消息。

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MessageActivity extends AppCompatActivity {

    Messenger sendService;


    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 11:
                    Log.e("client",msg.getData().getString("client"));
                    break;

            }
        }
    };

    ServiceConnection mConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            sendService=new Messenger(iBinder);
            Message mess=Message.obtain(null,10);
            Bundle mBundle=new Bundle();
            mBundle.putString("message","客户端发送的消息");
            mess.setData(mBundle);
            mess.replyTo=new Messenger(mHandler);    //将messenger一起发送到服务端,使服务端接收到消息后返回消息通知客户端
            try {
                sendService.send(mess);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        Intent service_Intent=new Intent(this,MessageService.class);
        bindService(service_Intent,mConnection,BIND_AUTO_CREATE);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }
}

3、一起来看看控制台的输出结果

09-28 03:34:07.236 12979-12979/com.ricky.messagedemo:remote1 E/service: 客户端发送的消息
09-28 03:34:07.266 12907-12907/com.ricky.messagedemo E/client: 服务端已接受到消息

没有更多推荐了,返回首页