Android AIDL实现跨进程Activity与Service的通信

本文深入解析Android Interface Definition Language (AIDL),阐述其在Android进程间通信中的作用,包括AIDL的基本概念、为何需要AIDL、如何使用AIDL进行跨进程通信,以及如何实现和解注册跨进程回调接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、什么是AIDL?

AIDL:Android Interface Definition Language 用于生成可以在Android设备上两个进程之间进行进程间通信(interprocess communication,IPC)的代码。

官方AIDL定义:

On Android, one process cannot normally access the memory of another process. So to talk, they need to decompose their objects into primitives that the operating system can understand, and marshall the objects across that boundary for you. The code to do that marshalling is tedious to write, so Android handles it for you with AIDL.

大概意思就是说Android进程之间不能直接通信,需要把对象转换成计算机能识别的原始语言,然后安排它跨越进程边界。但是做这些事很繁琐,于是Android提供了AIDL来做这件事。(换句话就是要实现跨进程需要编写很多复杂的代码,于是android提供了AIDL,通过编写简单的AIDL文件,编译器根据AIDL的规则生成那些复杂的代码)

二、为什么要有AIDL?

每个应用程序都运行在自己的独立进程中,并且可以启动另一个应用进程的服务,而且经常需要在不同的进程间传递数据对象。

在Android平台,一个进程不能直接访问另一个进程的内存空间,所以要想对话,需要将对象分解成操作系统可以理解的单元,并且有序的跨越进程边界。

三、AIDL的使用?

使用AIDL跨进程通信,整体过程和单进程一样,都是通过一个Binder来通信的,区别在于单进程的Binder是自己通过继承Binder类来手动实现的,而跨进程的Binder是通过AIDL自动生成的。

首先①新建一个AIDL文件

右键 -> new -> AIDL -> AIDL File,然后输入文件名点击finish完成(这里的示例代码是IMyAidlInterface)

上面的操作不管右键哪个目录,完成之后都会在src/main目录下生成了一个aidl目录,新建的IMyAidlInterface.aidl文件就在这个目录下,注意和eclipse的不同。

打开这个文件发现就是一个接口(可能会默认生成一个basicTypes方法,这是示例方法,不用管,可以删掉),然后在里面定义一个自己的方法(需要其他的方法的话自己看着加)

// IMyAidlInterface.aidl
package com.example.wcystart.wcystart;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
//    /**
//     * Demonstrates some basic types that you can use as parameters
//     * and return values in AIDL.
//     */
//    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
//            double aDouble, String aString);
void testMethod(String str);
}

②编译项目

Build -->Make Project

完成之后会在 app/build/generated/source/aidl/debug/ 目录下生成一个和AIDL文件同名的java文件 IMyAidlInterface.java

这个类文件就是用来提供进程间通信的,需要的Binder类就在这里面。

简单来说,AIDL就是一个用来生成代码的工具,最终的目的就是得到IMyAidlInterface.java这个类。这个和数据库框架GreenDao很像,都是通过一些简单的做法生成很多复杂而有用的代码,然后拿来直接用。

③分析IMyAidlInterface.java

生成的代码格式很乱,为了方便查看,ctrl+alt+L格式化一下。

/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: E:\\WcyStart\\app\\src\\main\\aidl\\com\\example\\wcystart\\wcystart\\IMyAidlInterface.aidl
 */
package com.example.wcystart.wcystart;
// Declare any non-default types here with import statements

public interface IMyAidlInterface extends android.os.IInterface {
    //内部抽象类

    /**
     * Local-side IPC implementation stub class.
     */
    public static abstract class Stub extends android.os.Binder implements com.example.wcystart.wcystart.IMyAidlInterface {
        private static final java.lang.String DESCRIPTOR = "com.example.wcystart.wcystart.IMyAidlInterface";

        /**
         * Construct the stub at attach it to the interface.
         */
        public Stub() {
            this.attachInterface(this, DESCRIPTOR);
        }

        /**
         * Cast an IBinder object into an com.example.wcystart.wcystart.IMyAidlInterface interface,
         * generating a proxy if needed.
         */
        public static com.example.wcystart.wcystart.IMyAidlInterface asInterface(android.os.IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin != null) && (iin instanceof com.example.wcystart.wcystart.IMyAidlInterface))) {
                return ((com.example.wcystart.wcystart.IMyAidlInterface) iin);
            }
            return new com.example.wcystart.wcystart.IMyAidlInterface.Stub.Proxy(obj);
        }

        @Override
        public android.os.IBinder asBinder() {
            return this;
        }

        @Override
        public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
            switch (code) {
                case INTERFACE_TRANSACTION: {
                    reply.writeString(DESCRIPTOR);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值