android AIDL使用demo

背景

最近打算学习一下如何在framework层添加一个自定义service。
了解到自定义service需要使用aidl,为了加强对aidl的了解和使用过程,特意又温习了一下aidl的使用,并用博客的形式记录下来。
aidl官方参考:https://developer.android.google.cn/develop/background-work/services/aidl?hl=en

AIDL是什么

官方解释:Android 接口定义语言 (AIDL) 类似于其他 IDL:它允许您定义客户端和服务使用进程间通信 (IPC) 进行相互通信时都认可的编程接口。

定义 AIDL 接口

project结构图
这里我使用的ide是Android Studio Iguana版本。
首先创建一个project,里面创建两个app一个叫client,另一个叫server。
在server app的src/main目录创建一个aidl文件IMyAidlInterface.aidl,内容如下

// IMyAidlInterface.aidl
package com.hai.server;
// Declare any non-default types here with import statements
//aidl说明参考:https://developer.android.google.cn/develop/background-work/services/aidl?hl=en
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);

    int getPid();

    oneway void getThreadId();
}

IMyAidlInterface.aidl中定义了三个接口供客户端调用。

然后编译一下project,就会在server的build目录下自动生成IMyAidlInterface.java接口文件,内容如下

/*
 * This file is auto-generated.  DO NOT MODIFY.
 */
package com.hai.server;
// Declare any non-default types here with import statements
//aidl说明参考:https://developer.android.google.cn/develop/background-work/services/aidl?hl=en
public interface IMyAidlInterface extends android.os.IInterface
{
   
   
  /** Default implementation for IMyAidlInterface. */
  public static class Default implements com.hai.server.IMyAidlInterface
  {
   
   
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException
    {
   
   
    }
    @Override public int getPid() throws android.os.RemoteException
    {
   
   
      return 0;
    }
    @Override public void getThreadId() throws android.os.RemoteException
    {
   
   
    }
    @Override
    public android.os.IBinder asBinder() {
   
   
      return null;
    }
  }
  /** Local-side IPC implementation stub class. */
  public static abstract class Stub extends android.os.Binder implements com.hai.server.IMyAidlInterface
  {
   
   
    /** Construct the stub at attach it to the interface. */
    public Stub()
    {
   
   
      this.attachInterface(this, DESCRIPTOR);
    }
    /**
     * Cast an IBinder object into an com.hai.server.IMyAidlInterface interface,
     * generating a proxy if needed.
     */
    public static com.hai.server.IMyAidlInterface asInterface(android.os.IBinder obj)
    {
   
   
      if ((obj==null)) {
   
   
        return null;
      }
      android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
      if (((iin!=null)&&(iin instanceof com.hai.server.IMyAidlInterface))) {
   
   
        return ((com.hai.server.IMyAidlInterface)iin);
      }
      return new com.hai.server.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
    {
   
   
      java.lang.String descriptor = DESCRIPTOR;
      if (code >= android.os.IBinder.FIRST_CALL_TRANSACTION && code <= android.os.IBinder
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值