AIDL的基本使用

對(duì)于AIDL的一些使用:最基礎(chǔ)使用、稍高級(jí)使用、......

基本概念

此章節(jié)可能和本文沒有太大關(guān)系,了解一下即可

圖片解釋

[圖片上傳失敗...(image-569908-1512305024994)]

全稱與中文名

  • IPC:Inter-Process Communication(進(jìn)程間通信)
  • Ashmem:Anonymous Shared Memory(匿名共享內(nèi)存)
  • Binder:Binder(進(jìn)程間通信機(jī)制)
  • AIDL:Android Interface Definition Language(android接口定義語言)
  • Intent:Intent(意圖)

基本概念

  • IPC:一種概念,即進(jìn)程間通信
  • Ashmem:作用之一是通過Binder進(jìn)程間通信機(jī)制來實(shí)現(xiàn)進(jìn)程間的內(nèi)存共享
  • Binder:對(duì)IPC的具體實(shí)行,是IPC的一種具體實(shí)現(xiàn)
  • AIDL:Binder機(jī)制向外提供的接口,目的是為了方便調(diào)用Binder
  • Intent:最高層級(jí)的封裝,實(shí)質(zhì)上封裝了對(duì)Binder的使用

注意

基本開發(fā)流程:先開發(fā)Service端,后開發(fā)Client端

使用AndroidStudio創(chuàng)建AidlDemo工程后,再在里面創(chuàng)建Service Module和Client Module,不用管默認(rèn)的app Module

開發(fā)流程-最基礎(chǔ)使用

分支:feature/aidl_base

Service端

包名:com.fqxyi.aidlservice

1、創(chuàng)建aidl文件,如:IAidlBinder.aidl,新增接口,如:String getInfo();

2、檢查build/generated/source/aidl/debug下是否存在對(duì)應(yīng)的java文件,若無則Rebuild Project,若出現(xiàn)錯(cuò)誤,請(qǐng)查看#FAQ章節(jié)

3、創(chuàng)建繼承于android.app.Service的Service類,如:AidlService.java,并實(shí)現(xiàn)必須要實(shí)現(xiàn)的onBind方法

3.1、在AndroidManifest.xml文件中靜態(tài)注冊(cè)Service,詳細(xì)注冊(cè)代碼如下:

<!-- exported表示是否支持其它應(yīng)用調(diào)用當(dāng)前組件 -->
<!-- process表示將組建運(yùn)行到單獨(dú)的進(jìn)程中 -->
<!-- action用于用于過濾識(shí)別其他的Intent -->
<service android:name=".AidlService"
  android:exported="true"
  android:process=":Remote">
  <intent-filter>
      <action android:name="com.fqxyi.aidlservice.remote"/>
  </intent-filter>
</service>

4、接著我們回到AidlService.java文件,onBind方法需要我們返回一個(gè)IBinder對(duì)象。顯然,到目前為止能夠得到IBinder對(duì)象的類只有通過IAidlBinder.aidl自動(dòng)生成的IAidlBinder.java類。

由于默認(rèn)代碼格式很亂,所以為了方便查看,我們可以使用快捷鍵格式化一下代碼:Ctrl(Command)+Alt(Option)+L

仔細(xì)閱讀代碼發(fā)現(xiàn),我們想要得到的IBinder對(duì)象是通過asBinder()方法返回的,所以接下來我們只需要返回一個(gè)IAidlBinder.Stub的對(duì)象就可以了。

5、實(shí)例化IAidlBinder.Stub的對(duì)象之后,我們可以處理我們自定義的方法getInfo(),比如最簡單的就是返回一串字符串:return "I'm a Server";

Client端

包名:com.fqxyi.aidlclient

1、將Service端的aidl文件,拷貝到main文件夾下,需要注意的是aidl文件的包名還是Service端的包名,具體目錄結(jié)構(gòu)如下:

aidlclient
    |--src
        |--main
            |--aidl
                |--com.fqxyi.aidlservice // Service端的包名
                    |--IAidlBinder.aidl

2、檢查build/generated/source/aidl/debug下是否存在對(duì)應(yīng)的java文件,若無則Rebuild Project,若出現(xiàn)錯(cuò)誤,請(qǐng)查看#FAQ章節(jié)

3、創(chuàng)建Intent對(duì)象并實(shí)例化,接著配置在Service端配置的action,實(shí)現(xiàn)Service的綁定,具體代碼如下所以:

Intent intent = new Intent();
intent.setAction("com.fqxyi.aidlservice.remote");
intent.setPackage("com.fqxyi.aidlservice");
bindService(intent, conn, Context.BIND_AUTO_CREATE);

4、上述代碼因不存在ServiceConnection而報(bào)錯(cuò),所以很簡單,我們需要?jiǎng)?chuàng)建一個(gè)ServiceConnection對(duì)象并實(shí)例化,接著在必須要實(shí)現(xiàn)的onServiceConnected(ComponentName name, IBinder service)方法中初始化IAidlBinder,在onServiceDisconnected(ComponentName name)方法中將IAidlBinder置為null。

仔細(xì)閱讀IAidlBinder.java代碼發(fā)現(xiàn),我們想要得到的IAidlBinder對(duì)象是通過asInterface(android.os.IBinder obj)方法返回的,需要傳入一個(gè)IBinder對(duì)象,所以接下來就很簡單了,只需要如下代碼即可:

IAidlBinder.Stub.asInterface(service);

5、最后我們只需要通過第4步得到的IAidlBinder對(duì)象,調(diào)用getInfo()方法,就可以得到內(nèi)容。

開發(fā)流程-稍高級(jí)使用

分支:feature/aidl_advanced

此處文檔說明基于#開發(fā)流程-最基礎(chǔ)使用章節(jié),通過列出不同點(diǎn)進(jìn)行說明。

Service端

包名:com.fqxyi.aidlservice

1、創(chuàng)建aidl文件,如:IAidlBinder.aidl,新增接口,如:String getInfo();Student getStudentInfo();

需要注意的是Student是我們自己定義的一個(gè)實(shí)現(xiàn)了Parcelable接口的Model類,在aidl文件中定義接口,需要我們手動(dòng)import語句,添加引用。

2、創(chuàng)建Student類,具體代碼如下所示:

package com.fqxyi.aidlservice.model;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by qingfeng on 2017/7/27.
 */

public class Student implements Parcelable {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    protected Student(Parcel in) {
        this.name = in.readString();
        this.age = in.readInt();
    }

    public static final Creator<Student> CREATOR = new Creator<Student>() {
        @Override
        public Student createFromParcel(Parcel in) {
            return new Student(in);
        }

        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }
}

大家如果不深究、只考慮使用的話,不用擔(dān)心自己會(huì)不會(huì)寫這些代碼,因?yàn)橹饕a是自動(dòng)生成的。

我們只需要定義一個(gè)name和age屬性,然后執(zhí)行以下兩部操作就行了:

  • 在構(gòu)造函數(shù)中讀取Parcel中的值并賦值給成員變量
  • 在writeToParcel方法中寫入值到Parcel中

3、創(chuàng)建Student.aidl文件,聲明Student實(shí)現(xiàn)了Parcelable接口,具體代碼如下所示:

// Student.aidl
package com.fqxyi.aidlservice.model;

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

import com.fqxyi.aidlservice.model.Student;

parcelable Student;

4、檢查build/generated/source/aidl/debug下是否存在對(duì)應(yīng)的java文件,若無則Rebuild Project,若出現(xiàn)錯(cuò)誤,請(qǐng)查看#FAQ章節(jié)

3、創(chuàng)建繼承于android.app.Service的Service類,如:AidlService.java,并實(shí)現(xiàn)必須要實(shí)現(xiàn)的onBind方法

3.1、在AndroidManifest.xml文件中靜態(tài)注冊(cè)Service,詳細(xì)注冊(cè)代碼如下:

<!-- exported表示是否支持其它應(yīng)用調(diào)用當(dāng)前組件 -->
<!-- process表示將組建運(yùn)行到單獨(dú)的進(jìn)程中 -->
<!-- action用于用于過濾識(shí)別其他的Intent -->
<service android:name=".AidlService"
  android:exported="true"
  android:process=":Remote">
  <intent-filter>
      <action android:name="com.fqxyi.aidlservice.remote"/>
  </intent-filter>
</service>

4、接著我們回到AidlService.java文件,onBind方法需要我們返回一個(gè)IBinder對(duì)象。顯然,到目前為止能夠得到IBinder對(duì)象的類只有通過IAidlBinder.aidl自動(dòng)生成的IAidlBinder.java類。

由于默認(rèn)代碼格式很亂,所以為了方便查看,我們可以使用快捷鍵格式化一下代碼:Ctrl(Command)+Alt(Option)+L

仔細(xì)閱讀代碼發(fā)現(xiàn),我們想要得到的IBinder對(duì)象是通過asBinder()方法返回的,所以接下來我們只需要返回一個(gè)IAidlBinder.Stub的對(duì)象就可以了。

5、實(shí)例化IAidlBinder.Stub的對(duì)象之后,我們可以處理我們自定義的方法getInfo(),比如最簡單的就是返回一串字符串:return "I'm a Server";

如果我們想要處理getStudentInfo()方法的話,可以在onCreate()方法中進(jìn)行初始化賦值操作,然后在getStudentInfo()方法中直接return即可。

Client端

包名:com.fqxyi.aidlclient

1、將Service端的aidl文件、和需要調(diào)用的文件,如:Student.java,拷貝到main文件夾下,需要注意的是這些文件的包名還是Service端的包名,具體目錄結(jié)構(gòu)如下:

aidlclient
    |--src
        |--main
            |--aidl
                |--com.fqxyi.aidlservice // Service端的包名
                    |--model
                        |--Student.aidl
                    |--IAidlBinder.aidl
            |--java
                |--com.fqxyi.aidlservice // Service端的包名
                   |--model
                       |--Student.java

2、檢查build/generated/source/aidl/debug下是否存在對(duì)應(yīng)的java文件,若無則Rebuild Project,若出現(xiàn)錯(cuò)誤,請(qǐng)查看#FAQ章節(jié)

3、創(chuàng)建Intent對(duì)象并實(shí)例化,接著配置在Service端配置的action,實(shí)現(xiàn)Service的綁定,具體代碼如下所以:

Intent intent = new Intent();
intent.setAction("com.fqxyi.aidlservice.remote");
intent.setPackage("com.fqxyi.aidlservice");
bindService(intent, conn, Context.BIND_AUTO_CREATE);

4、上述代碼因不存在ServiceConnection而報(bào)錯(cuò),所以很簡單,我們需要?jiǎng)?chuàng)建一個(gè)ServiceConnection對(duì)象并實(shí)例化,接著在必須要實(shí)現(xiàn)的onServiceConnected(ComponentName name, IBinder service)方法中初始化IAidlBinder,在onServiceDisconnected(ComponentName name)方法中將IAidlBinder置為null。

仔細(xì)閱讀IAidlBinder.java代碼發(fā)現(xiàn),我們想要得到的IAidlBinder對(duì)象是通過asInterface(android.os.IBinder obj)方法返回的,需要傳入一個(gè)IBinder對(duì)象,所以接下來就很簡單了,只需要如下代碼即可:

IAidlBinder.Stub.asInterface(service);

5、最后我們只需要通過第4步得到的IAidlBinder對(duì)象,調(diào)用getInfo()方法,就可以得到內(nèi)容,也可以調(diào)用getStudentInfo()方法,獲得Student對(duì)象。

FAQ

Service

ProcessException: Error while executing process aidl with arguments ...

Error:Execution failed for task ':aidlservice:compileDebugAidl'.
> java.lang.RuntimeException: com.android.ide.common.process.ProcessException: Error while executing process /qingfeng/work/sdk/build-tools/25.0.2/aidl with arguments {-p/qingfeng/work/sdk/platforms/android-25/framework.aidl -o/qingfeng/data/openSource/AidlDemo/aidlservice/build/generated/source/aidl/debug -I/qingfeng/data/openSource/AidlDemo/aidlservice/src/main/aidl -I/qingfeng/data/openSource/AidlDemo/aidlservice/src/debug/aidl -I/Users/qingfeng/.android/build-cache/92fb7eb4401d63eb124015b36c2a8a534302f1c9/output/aidl -d/var/folders/tq/f6kngw516g15xs24gdh80qvh0000gn/T/aidl1446680322459273337.d /qingfeng/data/openSource/AidlDemo/aidlservice/src/main/aidl/com/fqxyi/aidlservice/IAidlBinder.aidl}

出現(xiàn)此類問題,其實(shí)是因?yàn)槟愕腶idl文件編寫錯(cuò)誤,請(qǐng)仔細(xì)檢查:

  • 包名是否導(dǎo)入,是否正確導(dǎo)入
  • 其他原因...

Client

IllegalArgumentException: Service Intent must be explicit

FATAL EXCEPTION: main
Process: com.fqxyi.aidlclient, PID: 19282
java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.fqxyi.aidlservice.remote }
  at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1219)
  at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1318)
  at android.app.ContextImpl.bindService(ContextImpl.java:1296)
  ...

如果你使用的Android設(shè)備的版本大于5.0,則需要在bindService的時(shí)候,為你的intent添加以下語句:

intent.setPackage("com.fqxyi.aidlservice");

其他問題待收錄...

總結(jié)

什么?

代碼太多?

作者寫的太爛?看的頭昏腦漲...

沒關(guān)系,直接去看源碼就行了,記?。?/p>

最基礎(chǔ)使用分支:feature/aidl_base傳送門

稍高級(jí)使用分支:feature/aidl_advanced,傳送門

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容