Messenger类的使用

一、Messenger类

作用:类似Message类,但是是跨进程使用的。

解析:它的底层是由AIDL实现的,从构造方法可以看出

       //Service使用

        public Messenger(Handler target){

             mTarget = target.getIMessenger();   

        }

       //Client端使用

        public Messenger(IBinder targer){

          mTarget = IMessenger.Stub.asInterface(target);

        }

二、使用(Service与Client互相发送信息)

步骤一:Service端创建Messenger

public class BookService extends Service {
    private static final String TAG = "BookService";
    //①:创建Handler,为创建Messenger做准备
    private Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    //当客户端发送信息的时候吗,输出
            Bundle bundle = msg.getData(); Log.d(TAG,"Service收到消息"+bundle.getString("msg")); break; } } }; //②:创建Messenger private Messenger mMessenger = new Messenger(mHandler); @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { //③:将Messenger对象的Binder返回给Client return mMessenger.getBinder(); } }

步骤二:Client端接收Messenger发送信息给Service端

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private Messenger mService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //重新创建Messenger
                mService = new Messenger(service);
                //从Msg池中获取message
                Message msg = Message.obtain(null,0);
                //创建Bundle
                Bundle bundle = new Bundle();
                bundle.putString("msg","Hello,nice to meet you");
                //将Bundle装进message对象中
                msg.setData(bundle);
                try {
                    //将message发送给服务端,同时说明Message继承了Parcelable接口
                    mService.send(msg);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        Intent intent = new Intent(this,BookService.class);
        bindService(intent, connection, BIND_AUTO_CREATE);
    }

步骤三:实现服务器向客户端发送信息的双向交流

//Client端

/**
*第一步:创建Handler接收服务器发送过来的信息
*/
    private Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    Bundle bundle = msg.getData();
                    Log.d(TAG,"客户端接收到信息:"+bundle.getString("replyData"));
                    break;
            }
        }
    };

/**
*第二步:在 public void onServiceConnected(ComponentName name, IBinder service) 方法中添加
*/
msg.replyTo = mService;
/**
*Service端的Handler中
*/
 //在Handler中接收到Client中传送过来的Messenger
        Messenger messenger = msg.replyTo;
         //创建Msg
         Message replyMessage = Message.obtain(null,0);
         try {
           //将Msg发送回给Client
          messenger.send(replyMessage);
         } catch (RemoteException e) {
                 e.printStackTrace();
         }


流程图

 

posted @ 2016-04-04 16:29  技术丶从积累开始  阅读(606)  评论(0编辑  收藏  举报