一、概念
Messenger,可翻译为信使,通过它可以在不同进程中传递Message对象,在Message中放入我们需要传递的数据,就可以实现数据在进程间传递了。Messenger的底层实现是AIDL,它封装了Handler跟Binder。Messenger的构造方法如下:
//参数为Handler
public Messenger(Handler target) {
mTarget = target.getIMessenger();
}
//参数为IBinder
public Messenger(IBinder target) {
mTarget = IMessenger.Stub.asInterface(target);
}
二、使用
1.服务端进程
在服务端创建一个Service来处理客户端的连接请求,同时创建一个Handler并通过它来创建一个Messenger对象,然后在Service的onBind中返回这个Messenger对象底层的Binder即可。
2.客户端进程
绑定服务端的Service,绑定成功后用服务端返回的IBinder对象创建一个Messenger,通过这个Messenger就可以向服务端发送消息了,发消息类型为Message对象。如果需要服务端能够回应客户端,就和服务端一样,还需要创建一个Handler并创建一个新的Messenger,并把这个Messenger对象通过Message的replyTo参数传递给服务端,服务端通过这个replyTo参数就可以回应客户端。
三、例子
//服务端
public class MessengerService extends Service {
private static final String TAG = MessengerService.class.getSimpleName();
private static final int MSG_FROM_CLIENT = 0;
private static final int MSG_FROM_SERVER = 1;
private static class ServerHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_FROM_CLIENT:
Log.d(TAG, "zwm, MSG_FROM_CLIENT: " + msg.getData().getString("msg"));
Messenger clientMessenger = msg.replyTo;
Message replyMsg = Message.obtain(null, MSG_FROM_SERVER);
Bundle bundle = new Bundle();
bundle.putString("reply", "来自服务端");
replyMsg.setData(bundle);
try {
clientMessenger.send(replyMsg);
} catch (RemoteException e) {
e.printStackTrace();
}
break;
default:
super.handleMessage(msg);
}
}
}
private final Messenger mServerMessenger = new Messenger(new ServerHandler());
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "zwm, onBind");
return mServerMessenger.getBinder();
}
}
//客户端
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final int MSG_FROM_CLIENT = 0;
private static final int MSG_FROM_SERVER = 1;
private Messenger mServerMessenger;
private Messenger mClientMessenger = new Messenger(new ClientHandler());
private static class ClientHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_FROM_SERVER:
Log.d(TAG, "zwm, MSG_FROM_SERVER: " + msg.getData().getString("reply"));
break;
default:
super.handleMessage(msg);
}
}
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "zwm, onServiceConnected");
mServerMessenger = new Messenger(service);
Message msg = Message.obtain(null, MSG_FROM_CLIENT);
Bundle data = new Bundle();
data.putString("msg", "来自客户端");
msg.setData(data);
msg.replyTo = mClientMessenger;
try {
mServerMessenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "zwm, onServiceDisconnected");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "zwm, onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MessengerService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
Log.d(TAG, "zwm, onDestroy");
unbindService(mConnection);
super.onDestroy();
}
}
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tomorrow.messengertest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="MessengerService"
android:process=":MessengerService"/>
</application>
</manifest>