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传递数据
网友评论