Aidl的使用

1. 包名要一致

1.1 Aidl

a.服务端与客户端的Aidl包名要一致,不然将报错(2与4)

java.lang.SecurityException: Binder invocation to an incorrect interface


b.aidl的包名与当前用户的包名无关,引用即可(1与2,或者3与4)

如:com.example.IMyAidl.aidl,但当前包名为com.example.myserver


1.2 需要进行传递,并实现了Parcelable的类(这里是person)

    a.person只与当前端有关(包名),与对方包无关;内容还是一致的

    如client:com.example.client.bean, server:com.example.myserver.bean;

 b.person的包名要与当前端的包名一致,不然not found(1与2,或者3与4)

    aidl中com.example.client.bean.Person.aidl 

    Java中com.example.client.bean.Person.java 


2.如果需要更改包级,在类移动位置后,手动修改包名(2或者4)

3.使用 

服务器端:

public class XXXAidlService extends Service {
   

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
       
        return iBinder;
    }

   IBinder iBinder=new IMyAidl.Stub(){

      ........
   };
    
}
Application中
<service
    android:name=".service.XXXAidlService"
    android:enabled="true"
    android:exported="true" />//为true,其他进程才可以访问

 客户端:

IMyAidl iMyAidl;
private ServiceConnection conn=new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        
        iMyAidl = IMyAidl.Stub.asInterface(iBinder); //向下转型为对应的aidl实现类
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        iMyAidl=null;
    }
};
启动
 
private void bindServiceWithAidl() {
    Intent intent=new Intent();
    //打开外部应用
    intent.setComponent(new ComponentName("服务器包名","包名+具体位置+.XXAidlService"));
    bindService(intent,connAidl, Context.BIND_AUTO_CREATE);
}
记得在onDestroy()中解绑
unbindService(conn);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值