[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>
相关推荐
冻咸鱼1 小时前
MySQL中表操作
android·sql·mysql·oracle
技术小黑屋_1 小时前
从零搭建 Android ADB MCP Server:让 AI 助手直接操控你的 Android 设备
android·adb
2501_915921432 小时前
iOS混淆与IPA加固实战手记,如何构建苹果应用防反编译体系
android·macos·ios·小程序·uni-app·cocoa·iphone
Jeled2 小时前
Android 集成指南:Google 登录、Facebook 登录 与 Firebase 深入接入(实战)
android·kotlin·android studio·memcached·facebook
用户093 小时前
Android唤醒锁优化指南
android·面试·kotlin
aqi003 小时前
FFmpeg开发笔记(八十三)国产的视频裁剪框架AndroidVideoTrimmer
android·ffmpeg·音视频·流媒体
困到晕阙4 小时前
[Zer0pts2020]Can you guess it?
android
写点啥呢4 小时前
android取消每次u盘插入创建无用(媒体)文件夹
android·u盘·车机·aosp
杨筱毅4 小时前
【Android】RecyclerView LayoutManager 重写方法详解
android