美文网首页
利用Messenger实现同一进程Activity和Servic

利用Messenger实现同一进程Activity和Servic

作者: 吐痰高手 | 来源:发表于2016-12-20 22:05 被阅读688次

    今天突然发现使用Messenger不止可以实现进程间通信,它实际上是一种标准的通信模式,同一进程一样可以通信

    1.Activity向Service发送信息

    1) 首先在Service中定义一个Service专用的Handler,并重写onBind()方法将Handler包装之后返回

    public class MessengerService extends Service {
    
        Handler serviceHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            Messenger messenger = new Messenger(serviceHandler);
            return messenger.getBinder();       //Activity在bindService时就会获得这个IBinder对象
        }
    }
    

    2)通过bindService()在Activity中获取可以向Service发送消息的Messenger对象

    Messenger serviceMessenger; //在Activity中定义一个成员变量serviceMessenger,这个对象专门用来向Service发送信息
    
    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            serviceMessenger = new Messenger(service);      //这样在使用bindService()方法之后,serviceMessenger就可以向Service发送信息了
        }
    
        @Override
        public void onServiceDisconnected(ComponentName name) {
    
        }
    };
    

    发送信息

    private void sendMessage() {        //实际上这个message是发送到了Service的Handler中去
        Message message = Message.obtain();   
        try {
            serviceMessenger.send(message);   //因为Messenger可用于远程通信,所以要try-catch
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    

    2.Service向Activity发送信息

    1) 先在Activity中定义一个Handler用来接受Service发送的信息

    Handler activityHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    

    2) 既然已经有了向Service发送信息的能力了,那就直接把Handler包装在Messenger中发送给Service

    Messenger activityMessenger = new Messenger(activityHandler);   //包装Activity的handler
    Message message = Message.obtain();
    message.what = MessengerService.GET_ACTIVITY_MESSENGER;
    message.replyTo = activityMessenger;
    try {
        serviceMessenger.send(message);     //实际上是将message发送给Service的Handler处理
    } catch (RemoteException e) {
        e.printStackTrace();
    }
    

    3) Service获取Messenger

    public static final int GET_ACTIVITY_MESSENGER = -2;
    
    Messenger activityMessenger = null;
    
    Handler serviceHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case GET_ACTIVITY_MESSENGER:    //标识
                    activityMessenger = msg.replyTo;    //获取了Activity的Messenger
                    break;
            }
        }
    };
    

    4) Service向Activity发送信息

        private void sendSomethingToActivity(){
            Message message = Message.obtain();
            //这里添加message需要携带的信息
            try {
                activityMessenger.send(message);    //实际上是将message发送给了Activity的Handler来处理
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    

    3. 互相发送信息

    到这也应该明白了,要向对方发送信息,直接调用Messenger的send()方法就可以把携带信息的message发送给对方了

    //Activity发送给Service
    private void sendSomethingToService(){
        Message message = Message.obtain();
        //添加发送的信息到message中
        try {
            serviceMessenger.send(message);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    
    
    
    
    //Service发送给Activity
    private void sendSomethingToActivity(){
        Message message = Message.obtain();
        //添加发送的信息到message中
        try {
            activityMessenger.send(message);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    
    

    看起来写得麻烦,实际上降低了两个类的依赖,对以后的扩展很有帮助

    因为这个是个固定的写法,甚至可以直接写进BaseActivity中或者BaseService中

    相关文章

      网友评论

          本文标题:利用Messenger实现同一进程Activity和Servic

          本文链接:https://www.haomeiwen.com/subject/eapfvttx.html