Android IPC机制(二)用Messenger进行进程间通信 – ganchuanpu

Messenger可以在不同进程中传递Message对象,我们在Message中加入我们想要传的数据就可以在进程间的进行数据传递了。Messenger是一种轻量级的IPC方案并对AIDL 进行了封装,它实现起来比较容易,下面我们来看看如何实现。

首先我们先写服务端(MessengerService.java),在onBind方法中创建Messenger,关联接收消息的Handler调用getBinder来获取Binder对象,在handleMessage方法中接收客户端发来的信息。

服务端(MessengerService.java): 在handleMessage回调中收到客户端信息时,我们调用Message.replyTo得到客户端传过来的Messenger对象,创建消息并通过Messenger发送给客户端。

public class MessengerService extends Service {
    public static final String TAG = "MoonMessenger";
    public static final int MSG_FROMCLIENT=1000;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_FROMCLIENT:
                    Log.i(TAG,"服务端收到的信息-------"+msg.getData().get("msg"));
                    //得到客户端传来的Messenger对象
                    Messenger mMessenger=msg.replyTo;
                    Message mMessage=Message.obtain(null,MessengerService.MSG_FROMCLIENT);
                    Bundle mBundle=new Bundle();
                    mBundle.putString("rep","这里是服务端,我们收到信息了");
                    mMessage.setData(mBundle);
                    try {
                        mMessenger.send(mMessage);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        Messenger mMessenger=new Messenger(mHandler);
        return mMessenger.getBinder();
    }
}

客户端(MessengerActivity.java),客户端需要创建一个Handler来接收服务端的信息,前面我们在服务端调用Message.replyTo得到客户端传过来的Messenger对象,可是客户端并没有传送Messenger对象啊,现在我们加上这段代码将Messenger对象传给服务端,当然需要关联我们定义的Handler。

public class MessengerActivity extends AppCompatActivity {
    private Messenger mMessenger;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messenger);
        Intent intent=new Intent(MessengerActivity.this,MessengerService.class);
        bindService(intent,mServiceConnection, Context.BIND_AUTO_CREATE);
    }
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MessengerService.MSG_FROMCLIENT:
                    Log.i(MessengerService.TAG, "客户端收到的信息-------" + msg.getData().get("rep"));
                    break;
            }
        }
    };


private ServiceConnection mServiceConnection=new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mMessenger=new Messenger(service);
        Message mMessage=Message.obtain(null,MessengerService.MSG_FROMCLIENT);
        Bundle mBundle=new Bundle();
        mBundle.putString("msg", "这里是客户端,收到了嘛服务端");
        mMessage.setData(mBundle);
        //将Messenger传递给服务端
        mMessage.replyTo=new Messenger(mHandler);
        try {
            mMessenger.send(mMessage);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

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

    }
}

当然别忘了注册servce时要另开一个进程:

<service android:name=".MessengerService" android:process=":remote"></service>

   

运行代码查看Log,大功告成。 
这里写图片描述

 



You must enable javascript to see captcha here!

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress

无觅相关文章插件,快速提升流量