Android 中的各种aidl配置实现 总得来说是为了方便不同进程间进行通信,这是他的主要用途
自定义类型的数据类型要进行数据传递要实现Serializable接口或者android中特有的Parcelable接口
注意:如果在aidl接口中要传参数的话,必须手动写一下 数据bean的 readFromParcel方法。
public void readFromParcel(Parcel in) { content = in.readString(); time = in.readInt(); }aidl中的用in out inout tag进行操作
void setBeanIn(in MsgBean bean); void setBeanOut(out MsgBean bean); void setBeanInOut(inout MsgBean bean);
1.不同程序之间的aidl文件声明需保证文件包命和目录结构一致
2.服务端的声明的service 不要忘记在清单文件中配置(添加action等)
<service android:name=".services.AidlServices" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.startimes.aidl" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>
3.对于android5.0以上的系统来说在client端建立连接无法使用隐式启动模式,需要带有服务端ADILservice的action 和包命
解决办法如下:
1、通过显示意图启动Service(直接用类名);
2、如果想继续使用隐式意图的话,加上包名信息即可;
4.在client的conn中不能强制转换成aild文件的接口,应该按照aidl操作流程来
IMyAidlInterface.Stub.asInterface 实际上类似吧远程服务的binder接口转回为本地client的一个对象使用,相当于狸猫换太子
private IMyAidlInterface myAidlInterface; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { myAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);//得到该对象之后,我们就可以用来进行进程间的方法调用和传输啦。 try { Log.e("leslie", "content***" + content); } catch (RemoteException e) { Log.e("leslie", "content RemoteException***"); e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName componentName) { } };
网上找了一张图很好的说明了binder在aidl机制中的作用