[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>
相关推荐
深海呐3 小时前
Android AlertDialog圆角背景不生效的问题
android
ljl_jiaLiang3 小时前
android10 系统定制:增加应用使用数据埋点,应用使用时长统计
android·系统定制
花花鱼3 小时前
android 删除系统原有的debug.keystore,系统运行的时候,重新生成新的debug.keystore,来完成App的运行。
android
落落落sss4 小时前
sharding-jdbc分库分表
android·java·开发语言·数据库·servlet·oracle
消失的旧时光-19436 小时前
kotlin的密封类
android·开发语言·kotlin
服装学院的IT男7 小时前
【Android 13源码分析】WindowContainer窗口层级-4-Layer树
android
CCTV果冻爽9 小时前
Android 源码集成可卸载 APP
android
码农明明9 小时前
Android源码分析:从源头分析View事件的传递
android·操作系统·源码阅读
秋月霜风10 小时前
mariadb主从配置步骤
android·adb·mariadb
Python私教10 小时前
Python ORM 框架 SQLModel 快速入门教程
android·java·python