Messenger
Messenger是一种轻量级的IPC方案,底层实现是AIDL。它对AIDL进行了封装,由于它一次处理一个请求,因此在服务端我们不用考虑线程同步的问题,因为不存在并发执行的情形。
服务端进程:需要创建一个Service处理服务端的连接请求,同时创建一个Handler并通过它来创建一个Messenger对象,然后在Service的onBind中返回这个Messenger对象底层的Binder即可。
客户端进程:首先需要绑定服务端的Service,绑定成功后用服务端返回的IBinder对象创建Messenger,通过这个Messenger就可以向服务端发送消息了,发送消息类型就是Message对象。如果需要服务端回应,客户端一样需要创建一个Handler并通过它来创建一个Messenger对象,并把这个Messenger对象通过Message的replyTo参数传个服务器,服务器可以通过replyTo参数回应客户端。
首先来看服务端代码
public class MessengerService extends Service {
private static final String TAG = "MessengerService ";
private class MessengerHandler extends Handler {
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what) {
case 0:
//todo
//bundle msg.getData();
break;
default:
super.handleMessage(msg);
break;
}
}
}
private final Messenger mMessenger = new Messenger(new MessengerHandler());
@Override
public IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}
}
注册
<service
android:name=".MessengerService"
android:process=":remote"
/>
接下来看客户端代码,你会发现,基本和AIDL的过程差不多,只不过是做了封装,可以发送Message消息
public class MessengerActivity extends Activity{
private static final String TAG = "MessengerActivity ";
private Messenger mService;
private ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService=new Messenger(service);
Message message=new Message();
Bundle bundle=new Bundle();
bundle.putString("tes","data");
message.setData(bundle);
try {
mService.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=new Intent(this,MessengerService.class);
bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
}
}
在复杂一点就是双方进行即时通讯
服务端直接在replyTo回复
public class MessengerService extends Service {
private static final String TAG = "MessengerService ";
private class MessengerHandler extends Handler {
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what) {
case 0:
//todo
Messenger clint=msg.replyTo;
Message message=new Message();
Bundle bundle=new Bundle();
bundle.putString("replyTo","你的消息我已经收到");
message.setData(bundle);
try {
clint.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
break;
default:
super.handleMessage(msg);
break;
}
}
}
客户端,先看客户端代码在理解服务端就比较简单啦
public class MessengerActivity extends Activity{
private static final String TAG = "MessengerActivity ";
//客户端的消息处理对象
private final Messenger mGetReplyMessenger = new Messenger(new MessengerHandler());
private class MessengerHandler extends Handler {
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what) {
case 0:
//todo
Log.i(TAG,"收到服务器数据");
break;
default:
super.handleMessage(msg);
break;
}
}
}
private Messenger mService;
private ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService=new Messenger(service);
Message message=new Message();
Bundle bundle=new Bundle();
bundle.putString("tes","data");
message.setData(bundle);
//注意这一句,把客户端的消息处理对象放到参数里面,给服务器调用
message.replyTo=mGetReplyMessenger;
try {
mService.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=new Intent(this,MessengerService.class);
bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
}
}
最后是流程图