• 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. }

  • 相关阅读:
    djangorestframework-simplejwt
    Jenkins Job的Migrate之旅
    优雅而高效的JavaScript——Proxy 和 Reflect
    Netscape 浏览器
    Linux安装与卸载MySql
    Java学习笔记5.4.2 Map接口 - TreeMap类
    大数据(9e)图解Flink窗口
    ATA-2168高压放大器用途有哪些方面
    bug场景记录
    雷达阵列天线孔径的概念及其相关意义
  • 原文地址:https://blog.csdn.net/xiaowang_lj/article/details/127584423