[Android AIDL] --- AIDL工程搭建

0 AIDL概念

AIDL(Android Interface Definition Language)是一种 IDL 语言,用于生成可以在 Android 设备上两个进程之间进行进程间通信(IPC)的代码。 通过 AIDL,可以在一个进程中获取另一个进程的数据和调用其暴露出来的方法,从而满足进程间通信的需求。通常,暴露方法给其他应用进行调用的应用称为服务端,调用其他应用的方法的应用称为客户端,客户端通过绑定服务端的 Service 来进行交互。

官方文档中对 AIDL 有这样一段介绍:

c 复制代码
Using AIDL is necessary only if you allow clients from different
applications to access your service for IPC and want to handle
multithreading in your service. If you do not need to perform
concurrent IPC across different applications, you should create your
interface by implementing a Binder or, if you want to perform IPC, but
do not need to handle multithreading, implement your interface using a
Messenger. Regardless, be sure that you understand Bound Services
before implementing an AIDL.

第一句很重要,"只有当你允许来自不同的客户端访问你的服务并且需要处理多线程问题时你才必须使用AIDL",其他情况下你都可以选择其他方法,如使用 Messenger,也能跨进程通信。可见 AIDL 是处理多线程、多客户端并发访问的,而 Messenger 是单线程处理。

上面的概念摘抄自https://juejin.cn/post/7125316537749241864#heading-0

1 如何在android studio中搭建使用aidi的cs框架

1> 新建工程

新建完成之后是这样的

2> 修改moudle name,将默认的app修改为aidl_client

改完之后是这样的

3> 新建aidl_server module, File->New->Phone & Table Module-> next

建立好之后整个工程是这样的

4> 修改编译的app name

默认是这样的

将app修改为aidl_client

5> 在server端新建aidl文件

建好之后如下

我们要实现的接口方法都在aidl文件中实现即可,写好aidl文件之后,我们直接编译,切换到project视图,可以看到build目录生成与aidl文件对应的java文件。

6> 有了aidl接口之后,需要在server端新建service

在service类中实现刚才aidl的接口

7> 直接在文件中找到aidl文件目录,从aidl_server端,将aidl文件拷贝到aidl_client 端

拷贝到这里

8> 在client端调用server端实现的aidl方法时候,需要主要两个参数

在client端的这2个设置

c 复制代码
                    intent.setAction("com.example.aidl_server");
                    intent.setPackage("com.example.aidl_server");

action和package的参数需要与server端的AndroidManifest.xml中的参数对应

9> client端的AndroidManifest.xml修改,主要是要添加<uses-permission> 和<queries>

c 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.aidl_client">

    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
        tools:ignore="QueryAllPackagesPermission" />
    <queries>
        <package android:name="com.example.aidl_service"/>
    </queries>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Aidl">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
相关推荐
Wellijohn2 小时前
一个 Android 老兵的十年开源路:从 167 star 的滑动控件,到本地优先的 AI Agent
android·人工智能·开源
好的佩奇6 小时前
Dart 之元数据与注解
android·flutter·dart
三少爷的鞋8 小时前
很多开发者把物理上的分层,当成了架构
android
FungLeo9 小时前
FengCMS · PHP+MySQL 升级实录 · MySQL DDL不能回滚:模型字段结构操作的失败续做
android·mysql·php·工程实践·fengcms
雨白10 小时前
Android 建造者模式实战:从 AlertDialog 源码到 TopBar 的两种实现
android
_祝你今天愉快12 小时前
Android 高级绘制:[五] 贝塞尔曲线与计算规则
android
FungLeo1 天前
FengCMS · PHP+MySQL 升级实录 · 危险默认值之殇:root/123456、认证码 8888 写进配置加载器
android·mysql·php·安全加固·fengcms
yz_aiks1 天前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
booboosui1 天前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
雨白2 天前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android