Android AIDL示例及高版本问题
创始人
2025-05-29 04:39:27

文章目录

  • AIDL使用示例及高版本问题
    • 服务端app
    • 客户端app
    • 高版本问题
    • HarmonyOS上运行

AIDL使用示例及高版本问题

关于Android AIDL不论是官方文档,还是网络上的资料信息,都讲解的很完整,包括Binder机制。

这里只是将AIDL 跨进程调用进行一次使用的记录。

服务端app

1

在作为服务端的app,需要定义一个AIDL文件,即扩展名.aidl的接口文件,在文件内定义接口方法。

AIDL概览:传送门


interface ILogCollector {/*** Decimal: 65280*/const int TRANSACT_CODE_BASTIC_TYPES = 0xFF00;/*** Demonstrates some basic types that you can use as parameters* and return values in AIDL.*/void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,double aDouble, String aString) = 65280;
}

使用Android Studio 环境进行编译,会生成一个同名的Java接口文件ILogCollector.java

AIDL文件爱你定义
AIDL编译生成Java类

2

创建Service类,并在Service内实现AIDL接口编译生成的Stub类。

public class LogCollectorService extends Service {private static final String TAG = LogCollectorService.class.getSimpleName();/*** LogCollector服务端实现。*/private final ILogCollector.Stub mCollectorBinder = new ILogCollector.Stub() {@Overridepublic void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString)throws RemoteException {Log.d(TAG, "服务端 basicTypes() 方法被调用");}};@NonNull@Overridepublic IBinder onBind(Intent intent) {Log.i(TAG, "返回服务端Binder");return mCollectorBinder;}
}

3

在Manifest内注册Service,且可以定义一个对应的权限。




这样服务端最简单的app定义好了。

客户端app

创建一个新的app工程,用以测试app服务端。

AIDL客户端定义及调用传送门

在客户端实现中定义ServiceConnection,进行bindService()调用。

// MainActivity.java关键代码private ILogCollector mRemoteProxy;private final ServiceConnection mServConn = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {mRemoteProxy = ILogCollector.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {mRemoteProxy = null;}};// bindService()绑定远程服务if (v.getId() == R.id.button_bind) {Intent servIntent = new Intent("com.sanren1024.logcollector.LOG_SERVICE");servIntent.setClassName("com.sanren1024.logcollector", "com.sanren1024.logcollector.LogCollectorService");//            servIntent.setAction("com.sanren1024.logcollector.LOG_SERVICE");servIntent.setPackage("com.sanren1024.logcollector");boolean launchResult = bindService(servIntent, mServConn, Context.BIND_AUTO_CREATE);Toast.makeText(this, "启动结果:" + launchResult, Toast.LENGTH_SHORT).show();return;}// 方法调用if (v.getId() == R.id.button_invoke && mRemoteProxy != null) {try {mRemoteProxy.basicTypes(1, 2, false, 3.0f, 4.5d, "nicholas");} catch (RemoteException e) {throw new RuntimeException(e);}return;}// 解绑if (v.getId() == R.id.button_unbind && mRemoteProxy != null) {unbindService(mServConn);}

这样在运行时,客户端app即可以调用到服务端app。

高版本问题

上述的AIDL定义,程序实现,调用均在Android 8上执行,可以成功调用。

但在Android 12上测试存在服务端app无法被调用起的问题。

工程的 targetSDK 33的值是33,读了文章CSDN AIDL报错,bindService一直连接不上、不起作用,并查看官方文档及Android 11 中的软件包可见性可以知道原因。

在Android 11(API Level 30)以上,App可查询的应用列表被看作是私有敏感数据,即要查询或调用其他app时,会受到系统限制,也就是Android上对应用的访问查询做了更加严格的限制。

按照blog解释,基于对私有数据越来越严格的控制,且在一般场景下,app不需要完全查询调用一个设备上的其他所有的应用。因此Android 11上引入了app查询或与其他应用交互的新特性:在manifest中引入新标签,显式声明App需要查询/交互的其他应用。

可以查看官方的使用说明,解决在高版本Android上无法IPC调用的问题。

HarmonyOS上运行

目前,android app还是可以在HarmonyOS系统上运行,因此也在Huawei设备上运行,但没有成功,似乎是HMS框架的一个内部错误。

相关内容

热门资讯

折价仍遇冷!大通证券51.59... 7月9日,京东资产交易平台信息显示,大通证券51.59%股权二次拍卖于当日12时结束,因无投资者报名...
踩监管红线!7家民营银行上半年... 2026年以来,银行业严监管趋势持续。《国际金融报》记者梳理发现,2026年上半年,19家民营银行中...
汇率、合资、市场三重影响!江淮... 近日,安徽江淮汽车集团股份有限公司(下称“江淮”)发布了2026年半年度业绩预亏公告。公司预计202...
中国石化完成对中国航油重组 中... 记者从中国石化新闻办获悉,中国石化宣布完成对中国航油的重组工作,中国航油正式成为中国石化二级全资子公...
独家专访马其顿证券交易所CEO... 2026.07.11本文字数:2155,阅读时长大约4分钟作者 |第一财经 航宇、姜一鹤、钱晓鑫、杨...