1. Explanation of keywords oneway, in, out, and inout
1. oneway: Asynchronous execution, regardless of whether the server has completed execution, returns directly
2. in: Data can only flow from the client to the server.
3. out: Data can only flow out from the server to the client.
4. inout: data can flow in both directions between the server and the client
2. Binder two-way communication idea
The communication between the client and the server is actually to obtain the IBinder object of the server to operate the server.
Then the client can pass an IBinder object to the server in the same way, so that the server can operate the client.
3. The function of linktodeath
During two-way communication between the client and the server, when the server crashes, it can receive a callback.
DeathRecipient deathHandle = new DeathRecipient() {
@Override
public void binderDied() {
// TODO Auto-generated method stub
Log.i(TAG, "binder is died");
}
};
service.linkToDeath(deathHandle, 0);
4. Messenger (the principle is actually a simplified version of aidl cross-process communication based on aidl)
1. Server-side use of Messenger
The server needs to implement a Handler to handle cross-process communication information sent from the client:
Secondly, the server constructs the corresponding Messenger:
Server side structure:
Messenger messenger = new Messenger(messengerHandler);
Note that the parameter here is messengerHandler, which is a Handler
Finally, when the server's onBinder calls back, the Messenger's IBinder object must be returned to the client.
@Nullable
@Override
public IBinder onBind(Intent intent) {
return messenger.getBinder();
}
2. Use of client
The client still obtains the IBinder returned by the server through bindService in onServiceConnected of the ServiceConnection class as before, thereby obtaining the Messenger agent class of the server, and calls the send function to send the Message. Therefore, the only information that Messenger can send is the information that Message can carry.
Intent intent = new Intent(MainActivity.this,MessengerService.class);
Log.i("test","MessengerService onClick ");
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
try {
Log.i("test","MessengerService onServiceDisconnected name = " +name);
messengerServer = new Messenger(service);
sendMessageToServer();
} catch (Exception e) {
e.printStackTrace();
Log.i("test","error " ,e);
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("test","client onServiceDisconnected name = " +name);
}
}, BIND_AUTO_CREATE);
void sendMessageToServer() throws RemoteException {
Message toServer = Message.obtain();
toServer.replyTo = messengerClientSend;
toServer.what = 1;
//toServer.obj = "hello I send from client"; Note that non-parcel objects cannot be passed. This can only assign a parcel type object to obj, otherwise an error will be reported.
Bundle bundle = new Bundle();
bundle.putString("bundleKey","bundleValue Client");
toServer.setData(bundle);
messengerServer.send(toServer);
}
(messenger) use link - https://blog.csdn.net/learnframework/article/details/119845833