进程间通信AIDL汇总

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(直接用类名);

[java]  view plain  copy
  1. Intent intent = new Intent(com.yulore.test.AppService.class);  
  2. context.startService(intent);  


2、如果想继续使用隐式意图的话,加上包名信息即可;

[java]  view plain  copy
  1. Intent intent = new Intent();  
  2. intent.setAction("com.yulore.recognize.android");  
  3. intent.setPackage(context.getPackageName());    //兼容Android 5.0  
  4. context.startService(intent);  

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机制中的作用

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值