[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>
相关推荐
风浅月明5 小时前
[Android]应用内更新问题
android
当归10245 小时前
接雨水的算法
android·java·算法
猿小帅017 小时前
androidnetflix手机版遥控器操作
android·framework
l and7 小时前
Android Http-server 本地 web 服务
android
CYRUS STUDIO8 小时前
使用 AndroidNativeEmu 调用 JNI 函数
android·汇编·arm开发·arm·逆向·jni
消失的旧时光-19438 小时前
Android 串口通信
android
风浅月明8 小时前
[Android]使用WorkManager循环执行任务
android
_extraordinary_10 小时前
Linux权限(一)
android·linux·excel
人生!?10 小时前
给小米/红米手机root(工具基本为官方工具)——KernelSU篇
android·linux·智能手机
古苏11 小时前
Android输入事件传递流程系统源码级解析
android