aidl基础使用

Server端:
1.添加与java同级的aidl文件夹,创建aidl接口类并在类中定义传递信息的接口函数(注:接口类所在包名要与实现类包名一致)
图一

2.实现类实现aidl接口
public class MyService extends Service {
public MyService() {}
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
class MyBinder extends IMyAidlInterface.Stub{
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public String getName() throws RemoteException {
return “test”;
}
}
}

Client端:
1.复制导入和server端一致的aidl文件
图二

2.绑定关联server端服务接口
IMyAidlInterface iMyAidlInterface
Intent intent = new Intent();
intent.setPackage(“com.example.aidlservice”);
intent.setAction(“com.example.aidlservice.MyService”);
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
},BIND_AUTO_CREATE);
3.使用关联的server端接口
iMyAidlInterface.getName()

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值