AIDL詳解2——複雜數據通信

官方文檔中介紹:

在Android上,一個進程通常無法訪問另一個進程的內存,進程需要將其對象分解成操作系統能夠識別的原語,並將對象編組成跨越邊界的對象。我們可以通過AIDL來處理這個問題,我們必須使用到Parcelable接口。因爲Android系統可以通過它將對象分解成可編組到各進程的原語。(也就是將對象轉換成二進制代碼,即序列化)

Parcelable

實現Parcelable就是爲了序列化,Android中實現序列化有兩個選擇:一是實現Serializable接口(是JavaSE本身就支持的),一是實現Parcelable接口(是Android特有功能,效率比實現Serializable接口高效,可用於Intent數據傳遞,也可以用於進程間通信(IPC))。實現Serializable接口非常簡單,聲明一下就可以了,而實現Parcelable接口稍微複雜一些,但效率更高。

實現Serializable:

public class Person implements Serializable {
    private String name;
    private  int id;
    public Person(){}

}

實現Parcelable:
1)implements Parcelable
2)重寫writeToParcel方法,將你的對象序列化爲一個Parcel對象,即:將類的數據寫入外部提供的Parcel中,打包需要傳遞的數據到Parcel容器保存,以便從 Parcel容器獲取數據
3)重寫describeContents方法,內容接口描述,默認返回0就可以
4)實例化靜態內部對象CREATOR實現接口Parcelable.Creator
public static final Parcelable.Creator CREATOR

以上都是必須實現的

public class Person implements Parcelable {
    private String name;
    private  int id;
    public Person(){}

    protected Person(Parcel in) {
        name = in.readString();
        id = in.readInt();
    }

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

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

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

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

AIDL傳遞對象

首先在Java包下面創建一個Person對象,並實現Parcelable的接口。其實是將上面的內容具體化:

public class Person implements Parcelable {
    private String name;
    private  int id;
    public Person(){}

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public static Creator<Person> getCREATOR() {
        return CREATOR;
    }

    public Person(int id,String name) {
        this.name = name;
        this.id = id;
    }

    protected Person(Parcel in) {
        name = in.readString();
        id = in.readInt();
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {

        //從Parcel中取出對象
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in.readInt(),in.readString());
        }

        //對象數組
        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };

    //描述內容
    @Override
    public int describeContents() {
        return 0;
    }

    //將對象寫入Parcel
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(id);
    }
}

我們在AIDL下面創建Person.aidl,由於前面創建了Person類,所以我在創建Perison.aidl時,不能創建。於是我先創建爲IPerson.aidl,然後改名爲Person。

// Person.aidl
package com.example.aidldemo;
parcelable Person;

這裏寫圖片描述

接下來創建一個IGetPerson的AIDL,實現獲取Peron的方法,這裏要注意,我們的Person需要導包,由於前面我們創建了Person.aidl,所以能夠使用import將Person包導入進來。前面提到過,我們通過客戶端輸入,所以參數使用了in。
還需注意的是,每次創建完AIDL,都需要運行下,才能讓Android系統爲我們創建相應的java接口。

package com.example.aidldemo;
import com.example.aidldemo.Person;

interface IGetPerson {
List<Person> getPerson(in Person p);//參數中使用in
}

創建Service,並在Manifest中註冊:

public class PersonService extends Service {
    List<Person> persons = new ArrayList<>();

    public PersonService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return  mbinder;//返回mbinder
    }


    private final IGetPerson.Stub mbinder = new IGetPerson.Stub() {
        @Override
        public List<Person> getPerson(Person p) throws RemoteException {
            persons.add(p);//將Person加入到鏈表中
            return persons;
        }
    };
}

在客戶端的主要代碼爲:

  //點擊事件
  public void startService(View view) {
        try {
            Person person = new Person(1,"Jack");

            list = iGetPerson.getPerson(person);//調用AIDl中的方法
            //輸出內容
            StringBuilder builder = new StringBuilder();
            for (Person p : list) {
                builder.append(p.getId()).append(p.getName()).append(",");
            }
            textView.setText(builder);


        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

效果如圖:
這裏寫圖片描述

全部代碼:

package com.example.aidldemo;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private TextView textView ;
    private  IGetPerson iGetPerson;
    private List<Person> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.text);

        Intent intent = new Intent(MainActivity.this, PersonService.class);
        bindService(intent, connection, Context.BIND_AUTO_CREATE);

    }

    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iGetPerson = IGetPerson.Stub.asInterface(service);

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            iGetPerson = null;
        }
    };

    public void startService(View view) {
        try {
            Person person = new Person(1,"Jack");

            list = iGetPerson.getPerson(person);//調用AIDl中的方法
            //輸出內容
            StringBuilder builder = new StringBuilder();
            for (Person p : list) {
                builder.append(p.getId()).append(p.getName()).append(",");
            }
            textView.setText(builder);


        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

佈局代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.aidldemo.MainActivity">

  <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:onClick="startService"
      />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        />

</LinearLayout>

這些都是我對AIDL的理解。

發佈了43 篇原創文章 · 獲贊 13 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章