Android-IPC III IPC Mode in Android

Keywords: socket Linux

Mode 1 - Bundle

Bundle supports Bundle data transfer in Intent by using the three components of Bundle's four major Soviet construction projects. Bundle implements Parcelable interface, which can be easily transmitted between different processes.

Mode 2 - File Sharing

By streaming data, the Linux system allows concurrent reading/writing of files to proceed without restrictions.
The disadvantage is that if you read and write concurrently, the content is probably not up-to-date, so avoid concurrency.

Mode 3 - Use Messenger

Messenger is a lightweight IPC solution. The underlying implementation is AIDL.
It is characterized by processing one request at a time without considering thread synchronization at the server side.
Implementing a Messenger with both server and client
Server side
Create a Service to handle client connection requests, and create a Handler to create a Messenger Duixiang through it

public class MessengerService extends Service {

    private static final String TAG = "MessengerService";

    //Processing messages sent by clients
    private static class MessengerHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MyConstants.MSG_FROM_CLIENT:
                Log.i(TAG, "receive msg from Client:" + 
                        msg.getData().getString("msg"));
               /* Messenger client = msg.replyTo;
                Message relpyMessage = Message.obtain(null, MyConstants.MSG_FROM_SERVICE);
                Bundle bundle = new Bundle();
                bundle.putString("reply", "Well, I have received your message and will reply to you later. "";
                relpyMessage.setData(bundle);*/
                try {
                    client.send(relpyMessage);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            default:
                super.handleMessage(msg);
            }
        }
    }
    //Pass the message sent by the client to MessengerHandler
    private final Messenger mMessenger = new Messenger(new MessengerHandler());

    //Return the Binder object
    @Override
    public IBinder onBind(Intent intent) {
        return mMessenger.getBinder();
    }
}

Client
First bind the Service on the server side, and then create a Messenger on the returned IBinder object, through which the message can be sent to the server side.

    private Messenger mService;
    private Messenger mGetReplyMessenger = new Messenger(new MessengerHandler());

    private static class MessengerHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MyConstants.MSG_FROM_SERVICE:
                Log.i(TAG, "receive msg from Service:" + msg.getData().getString("reply"));
                break;
            default:
                super.handleMessage(msg);
            }
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mService = new Messenger(service);
            //Transfer via Message
            Message msg = Message.obtain(null, MyConstants.MSG_FROM_CLIENT);
            Bundle data = new Bundle();
            data.putString("msg", "hello, this is client.");
            msg.setData(data);
            msg.replyTo = mGetReplyMessenger;
            try {
                mService.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        public void onServiceDisconnected(ComponentName className) {
        }
    };

When the server returns data to the client, it should pay attention to:
msg.replyTo=mMessenger;

The disadvantage is that it can only process messages one by one, which is not appropriate if there are a lot of requests.

Mode 4 - ContentProvider

The underlying implementation also uses Bundle to achieve cross-process communication through the ContentProvider of the four components.

Mode 5 - Use Socket

Socket itself can support the transmission of arbitrary byte streams. Socket is a good IPC mode.

Posted by Baseball on Fri, 05 Apr 2019 15:24:30 -0700