Messenger可以在不同进程中传递Message对象,我们在Message中加入我们想要传的数据就可以在进程间的进行数据传递了。Messenger是一种轻量级的IPC方案并对AIDL 进行了封装,它实现起来比较容易,下面我们来看看如何实现。
首先我们先写服务端(MessengerService.java),在onBind方法中创建Messenger,关联接收消息的Handler调用getBinder来获取Binder对象,在handleMessage方法中接收客户端发来的信息。
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
public class MessengerService extends Service {
public static final String TAG = "MoonMessenger";
public static final int MSG_FROMCLIENT=1000;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_FROMCLIENT:
Log.i(TAG,"收到客户端信息-------"+msg.getData().get("msg"));
break;
}
}
};
@Override
public IBinder onBind(Intent intent) {
return new Messenger(mHandler).getBinder();
}
}
当然别忘了注册servce时要另开一个进程:
<service android:name=".MessengerService" android:process=":remote"></service>
接下来创建客户端(MessengerActivity.java),绑定另一个进程的servce,绑定成功以后根据服务端返回的Binder对象创建Messenger,并用Messenger向服务端发送信息。
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MessengerActivity extends AppCompatActivity {
private Messenger mMessenger;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messenger);
Intent intent=new Intent(MessengerActivity.this,MessengerService.class);
bindService(intent,mServiceConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection mServiceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mMessenger=new Messenger(service);
Message mMessage=Message.obtain(null,MessengerService.MSG_FROMCLIENT);
Bundle mBundle=new Bundle();
mBundle.putString("msg","这里是客户端,收到了嘛服务端");
mMessage.setData(mBundle);
try {
mMessenger.send(mMessage);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mServiceConnection);
}
}
服务端现在收到了客户端的消息,但是服务端现在无法回应客户端,现在我们来修改代码使服务端回应客户端,客户端也能收到服务端的回应。
首先我们修改服务端(MessengerService.java),在handleMessage回调中收到客户端信息时,我们调用Message.replyTo得到客户端传过来的Messenger对象,创建消息并通过Messenger发送给客户端。
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_FROMCLIENT:
Log.i(TAG,"收到客户端信息-------"+msg.getData().get("msg"));
//得到客户端传来的Messenger对象
Messenger mMessenger=msg.replyTo;
Message mMessage=Message.obtain(null,MessengerService.MSG_FROMCLIENT);
Bundle mBundle=new Bundle();
mBundle.putString("rep","这里是服务端,我们收到信息了");
mMessage.setData(mBundle);
try {
mMessenger.send(mMessage);
} catch (RemoteException e) {
e.printStackTrace();
}
break;
}
}
};
最后我们修改客户端(MessengerActivity.java),客户端需要创建一个Handler来接收服务端的信息
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case MessengerService.MSG_FROMCLIENT:
Log.i(MessengerService.TAG, "收到服务端信息-------" + msg.getData().get("rep"));
break;
}
}
};
前面我们在服务端调用Message.replyTo得到客户端传过来的Messenger对象,可是客户端并没有传送Messenger对象啊,现在我们加上这段代码将Messenger对象传给服务端,当然需要关联我们定义的Handler。(MessengerActivity.java)
private ServiceConnection mServiceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mMessenger=new Messenger(service);
Message mMessage=Message.obtain(null,MessengerService.MSG_FROMCLIENT);
Bundle mBundle=new Bundle();
mBundle.putString("msg", "这里是客户端,收到了嘛服务端");
mMessage.setData(mBundle);
//将Messenger传递给服务端
mMessage.replyTo=new Messenger(mHandler);
try {
mMessenger.send(mMessage);
} catch (RemoteException e) {
e.printStackTrace();
}
}
网友评论