[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>
相关推荐
zhangphil30 分钟前
Android GPU的RenderThread Texture upload上传Bitmap优化prepareToDraw
android
柿蒂2 小时前
聊聊SliverPersistentHeader优先消费滑动的设计
android·flutter
假装多好1233 小时前
android三方调试几个常用命令
android·1024程序员节·三方,gms
侧耳4293 小时前
android11禁止安装apk
android·java·1024程序员节
JohnnyDeng944 小时前
ArkTs-Android 与 ArkTS (HarmonyOS) 存储目录全面对比
android·harmonyos·arkts·1024程序员节
2501_915918415 小时前
iOS 26 查看电池容量与健康状态 多工具组合的工程实践
android·ios·小程序·https·uni-app·iphone·webview
limingade5 小时前
手机摄像头如何识别体检的色盲检查图的数字和图案(下)
android·1024程序员节·色盲检查图·手机摄像头识别色盲图案·android识别色盲检测卡·色盲色弱检测卡
文火冰糖的硅基工坊5 小时前
[嵌入式系统-150]:智能机器人(具身智能)内部的嵌入式系统以及各自的功能、硬件架构、操作系统、软件架构
android·linux·算法·ubuntu·机器人·硬件架构
2501_915909066 小时前
iOS 架构设计全解析 从MVC到MVVM与使用 开心上架 跨平台发布 免Mac
android·ios·小程序·https·uni-app·iphone·webview
明道源码7 小时前
Android Studio 创建 Android 模拟器
android·ide·android studio