Communication between Android processes--Messager detailed tutorial, two app implementations

Messenger is a lightweight IPC solution, and its underlying implementation is actually AIDL. When Messenger is used for cross-process communication, Messenger will add all service calls to the queue, and then the server will process one call at a time, and there will be no simultaneous calls. AIDL may be executed by multiple calls at the same time, and multi-threading issues must be dealt with.

step details

  • 1. Server
public class MyMessagerService extends Service {
    
    
    @SuppressLint("HandlerLeak")
    private Messenger messengerServices=new Messenger(new Handler(){
    
    
        @Override
        public void handleMessage(@NonNull Message msg) {
    
    
            super.handleMessage(msg);
            switch (msg.what){
    
    
                case 111://接收客户端发的消息
                    Log.e("TAG","获取客户端的name---"+msg.getData().getString("name"));//打印的数据:E/TAG: name---花花
                    Log.e("TAG","获取客户端的age---"+msg.getData().getInt("age",0));//打印的数据:E/TAG: age---2
                    Message message=new Message();
                    message.what=222;
                    Bundle bundle=new Bundle();
                    bundle.putString("ceshi","测试");
                    message.setData(bundle);
                    try {
    
    
                        msg.replyTo.send(message);//向客户端发消息
                    } catch (RemoteException e) {
    
    
                        throw new RuntimeException(e);
                    }
                    break;
            }
        }
    });

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

Messager.replyTo: Point to the Messenger of the client, and the Messenger holds an IBinder object (MessengerImpl) of the client, and the server uses this IBinder object to communicate with the client.

  • registration service
    insert image description here
  • 2. Client
	//给一个点击事件去绑定服务
	public void btn6(View view) {
    
    
		Intent intent = new Intent();
        intent.setAction("com.example.jinc2application.MyMessagerService");
        intent.setPackage("com.example.jinc2application");//包名
        bindService(intent, myMeaaengerConnection, Service.BIND_AUTO_CREATE);
	}
    private Messenger messenger;
    private ServiceConnection myMeaaengerConnection=new ServiceConnection() {
    
    
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
    
    
            messenger=new Messenger(iBinder);
            Message message=new Message();
            message.what=111;
            Bundle bundle=new Bundle();
            bundle.putString("name","花花");
            bundle.putInt("age",2);
            message.setData(bundle);
            message.replyTo=mMessengerHandler;
            try {
    
    
                messenger.send(message);
            } catch (RemoteException e) {
    
    
                throw new RuntimeException(e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
    
    

        }
    };

	@SuppressLint("HandlerLeak")
    private Messenger mMessengerHandler=new Messenger(new Handler(){
    
    
        @Override
        public void handleMessage(@NonNull Message msg) {
    
    
            super.handleMessage(msg);
            switch (msg.what){
    
    
                case 222:
                    Log.e("TAG","获取服务端的数据---"+msg.getData().getString("ceshi"));//打印的数据:E/TAG:---测试
                    break;
            }

        }
    });

Guess you like

Origin blog.csdn.net/afufufufu/article/details/131806571