• AIDL的使用


    AIDL是安卓接口定义语言的简称,用于进程间通信 ,

    AIDL默认支持的数据类型有:八种基本数据类型、String、List

    如果要使用AIDL传递类对象,就必须让类实现Parcelable接口

     文件结构如图所示

    1.创建自定义类型

    people.java

    1. public class People implements Parcelable {
    2. String name;
    3. int age;
    4. public People(int age,String name){
    5. this.age = age;
    6. this.name = name;
    7. }
    8. public People(Parcel in) {
    9. name = in.readString();
    10. age = in.readInt();
    11. }
    12. public static final Creator CREATOR = new Creator() {
    13. @Override
    14. public People createFromParcel(Parcel in) {
    15. return new People(in);
    16. }
    17. @Override
    18. public People[] newArray(int size) {
    19. return new People[size];
    20. }
    21. };
    22. @Override
    23. public int describeContents() {
    24. return 0;
    25. }
    26. @Override
    27. public void writeToParcel(Parcel dest, int flags) {
    28. dest.writeString(name);
    29. dest.writeInt(age);
    30. }
    31. }

     people.aidl

    1. package com.example.aidl;
    2. parcelable People;

    IPeopleManager.aidl

    1. package com.example.aidl;
    2. // Declare any non-default types here with import statements
    3. import com.example.aidl.People;
    4. interface IPeopleManager {
    5. /**
    6. * Demonstrates some basic types that you can use as parameters
    7. * and return values in AIDL.
    8. */
    9. void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
    10. double aDouble, String aString);
    11. int getAge();
    12. String getName();
    13. void addPeople(in People people);
    14. }

    aidl方法传入自定义类对象时,in、out、inout必须写(aidl默认支持的类型不用写,默认且只能是in),否则报错。

    .27-34: 'com.example.aidl.People' can be an out type, so you must declare it as in, out, or inout.

    关于参数前的in、out和inout,跨进程时,in参数会把参数的内容传给aidl,但其改动不会同步到调用进程;out参数不会把参数的属性传给aidl(aidl获取的参数对象属性为空),但其改动会同步到调用进程;inout参数则是in和out的综合。不跨进程时,三者则是摆设
     

     

     然后编译项目,如果提示找不到符号
      public void addPeople(com.example.aidl.People people) throws android.os.RemoteException;

    则需要在build.gradle 的android标签下文件中加入如下代码

    1. sourceSets {
    2. main {
    3. manifest.srcFile 'src/main/AndroidManifest.xml'
    4. java.srcDirs = ['src/main/java', 'src/main/aidl']
    5. resources.srcDirs = ['src/main/java', 'src/main/aidl']
    6. aidl.srcDirs = ['src/main/aidl']
    7. res.srcDirs = ['src/main/res']
    8. assets.srcDirs = ['src/main/assets']
    9. }
    10. }

  • 相关阅读:
    【第3节】“茴香豆“:搭建你的 RAG 智能助理
    支持向量机基本原理,Libsvm工具箱详细介绍,基于支持向量机SVM的遥感图像分类
    CSS Grid详解
    Spring系列:基于XML的方式构建IOC
    C/C++数据结构——列出连通集(深搜和广搜)
    公众号爆文写作怎么做?或许这些领域才适合你!
    kotlin协程并发/并行与串行互相切换,CoroutineScope与await
    【云原生】SpringCloud系列之客户端负载均衡Ribbon
    BAT大厂Java面试,如何抓住面试重点知识?收割大厂offer
    昨天的事情想说一下
  • 原文地址:https://blog.csdn.net/xiaowang_lj/article/details/127584423