Messenger

作者: LeoFranz | 来源:发表于2019-07-27 12:11 被阅读0次

Messenger是Android IPC体系中的一种工具,它底层封装了AIDL实现机制,能够实现客户端和服务端的非线性消息处理,即一次处理一个请求,在服务端不考虑并发情形。
基本流程:

服务端(包含handler对象实现)——(Messenger底层的Binder)——客户端(通过绑定服务获取Binder并构造Messenger对象,并向服务端发送消息)
客户端(包含handler对象实现)—— (Messege.replyTo包含Messenger)——服务端(获取客户端messenger向客户端发送消息)

具体实现代码

public class ClientActivity extends AppCompatActivity {

    private static final String TAG = "ClientActivity";
    private Button mStartButton;
    private Messenger mMessenger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mStartButton = findViewById(R.id.start_btn);
        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ClientActivity.this,MessagerService.class);
                bindService(intent,mServiceConnection,BIND_AUTO_CREATE);
            }
        });
    }

    private class ClientHander extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case MSG_FROM_SERVICE :
                    Log.d(TAG, "handleMessage: Client has received a msg from Service."
                            +msg.getData().getString("msgFromService"));
                    break;
                default:break;
            }
        }
    }

    ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mMessenger = new Messenger(service);
            Message message = Message.obtain(null,Utils.MSG_FROM_CLIENT);
            Bundle bundle =new Bundle();
            bundle.putString("msgFromClient","是是是男人就坚持五秒");
            message.setData(bundle);
            message.replyTo = new Messenger(new ClientHander());
            try {
                mMessenger.send(message);
            }catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mServiceConnection);
    }
}
//服务端在xml中的配置
<service android:name=".MessagerService"
                 android:process=":remote"/>
//具体实现代码
public class MessagerService extends Service {

    private static final String TAG = "MessagerService";

    public static class MessagerHander extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case Utils.MSG_FROM_CLIENT:
                    Log.i(TAG,"Service has rececived msg from client: "
                            +msg.getData().getString("msgFromClient"));
                    Messenger clientMessenger = msg.replyTo;
                    Message message = Message.obtain(null,Utils.MSG_FROM_SERVICE);
                    Bundle bundle = new Bundle();
                    bundle.putString("msgFromService","我***五秒怎么不能坚持");
                    message.setData(bundle);
                    try {
                        clientMessenger.send(message);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
                    default:
                        break;
            }
        }
    }

    private final Messenger mMessenger = new Messenger(new MessagerHander());

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mMessenger.getBinder();
    }
}
07-27 11:50:58.945 538-538/? I/MessagerService: Service has rececived msg from client: 是是是男人就坚持五秒
07-27 11:50:58.946 32698-32698/com.restart.leo.criminalintents D/ClientActivity: handleMessage: Client has received a msg from Service.我***五秒怎么不能坚持

注意事项:

  • Messenger和Message都实现了ParceLable接口,因此可以跨进程传输
  • Message.obj字段在同一个进程中很实用,但是跨进程时只能使用系统自带的实现了ParceLable接口的对象,所以建议使用Bundle传递数据

相关文章

  • Messenger轻量级IPC方案

    概述 什么是messenger Messenger可以翻译为信使,通过它可以在不同的进程中传递Messenger对...

  • Android跨进程通信之Messenger

    [Messenger简介] [代码][实现service端][实现client端] [效果] Messenger简...

  • MIXIN messenger 常见问题释疑

    MIXIN messenger 常见问题释疑 MIXIN messenger 只是建立在MIXIN网络上的第...

  • My My

    Glimmer Serenade Messenger

  • Messenger

    定义:是一种轻量级IPC方案,它的底层实现是AIDL,通过它可以在不同的进程中传递Message对象。使用方法:(...

  • Messenger

    Messenger是Android IPC体系中的一种工具,它底层封装了AIDL实现机制,能够实现客户端和服务端的...

  • 三步安装MIXIN Messenger,体验区块链版的“微信”

    一、什么是MIXIN Messenger MIXIN Messenger,是一款基于MIXIN公链的DAPP。她可...

  • IPC之Messenger

    参考Android开发艺术探索 Messenger Messenger:信使,通过它可以不同进程中传递Messag...

  • Android IPC 通信之 Messenger

    说到 IPC 除了我们之前讲的 AIDL ,经常被提及的还有 Messenger ,Messenger 底层是通过...

  • Android IPC之Messenger

    Messenger 翻译为信使,通过Messenger可以实现在不同进程间传递Message对象。Messenge...

网友评论

      本文标题:Messenger

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