1.Messenger进程间通信时,bundle有Parcelable对象解析数据的时候,需要调用bundle.setClassLoader方法,否则可能出现类型转换异常,因为bundle在调用getXXX
解析数据的时候,调用Parcelable的readParcelable方法,readParcelableCreator会判断传入的ClassLoader是否为空,为空是使用的是Parcel的ClassLoader,用来加载我们自定义的类就会报错。
示例:
客户端代码:
public class ClientActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "ClientActivity";
@BindView(R.id.send)
Button send;
private Messenger sendMessager;
private Intent intent;
private Messenger getMessenger = new Messenger(new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
bundle.setClassLoader(getClass().getClassLoader());//防止出现无法识别object的异常
Log.d(TAG, "收到服务端消息: " + bundle.getString("rp"));
}
});
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
sendMessager = new Messenger(service);
Message message = Message.obtain(null, 0);
Bundle bundle = new Bundle();
Data data = new Data();
data.setAddress("天安门");
data.setName("张三");
data.setBankCard("6221889292992922");
data.setBankOrg("中国农业银行");
data.setEmail("1818181919@qq.com");
data.setHeight(1.82);
data.setIdCard("828992988889999X");
data.setNation("汉");
data.setOld(38);
data.setPhone("18929929922");
data.setQqNum("982892992");
data.setSex("男");
data.setWeight(148.2);
bundle.putParcelable("data", data);
message.setData(bundle);
message.replyTo = getMessenger;//带上客户端监听
try {
sendMessager.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
ButterKnife.bind(this);
intent = new Intent(this, ServerService.class);
send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: 准备发送");
try {
unbindService(serviceConnection);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
unbindService(serviceConnection);
super.onDestroy();
}
}
服务端代码:
public class ServerService extends Service {
private static final String TAG = "ServerService";
private Messenger messenger = new Messenger(new MsgHandler());
private class MsgHandler extends Handler{
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
bundle.setClassLoader(getClass().getClassLoader());
Data data = bundle.getParcelable("data");
Log.d(TAG, "来自客户端的消息: "+data.toString());
Messenger client = msg.replyTo;
Message message = Message.obtain(null,1);
Bundle bundle1=new Bundle();
bundle1.putString("rp","收到了!");
message.setData(bundle1);
try {
client.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
@Override
public IBinder onBind(Intent intent) {
return messenger.getBinder();
}
}
AndroidManifest声明:
<activity android:name=".process_communication.messager.ClientActivity" />
<service
android:name=".process_communication.messager.ServerService"
android:enabled="true"
android:exported="true"
android:process=":server" />
网友评论