最終更新:2017-03-28 (火) 18:53:49 (2577d)
AIDL
Top / AIDL
Android Interface Definition Language
http://developer.android.com/guide/components/aidl.html
Android 搭載のデバイス上での 2 つのプロセスが、プロセス間通信 (IPC) を使って会話できるようにするためのコードを生成する目的するに使用される IDL言語
仕様
- http://www.techdoctranslator.com/android/appendix/aidl
- https://developer.android.com/guide/components/aidl.html
定義
- AIDL インターフェースは、Java プログラミング言語の構文を使用して .aidl ファイルで定義します。
- このファイルは、サービスをホストするアプリと、サービスにバインドするすべてのアプリのソースコード(src/ ディレクトリ内)に保存します。
概要
- .aidlファイルを含むアプリをビルドすると、Android SDKツールが .aidl ファイルに基づいて IBinder インターフェースを生成し、その結果をプロジェクトの gen/ ディレクトリに保存します。
- サービスは、必要に応じて IBinder インターフェースを実装します。 その後、クライアント アプリケーションをサービスにバインドし、IBinder からメソッドを呼び出して IPC を実行できるようになります。
定義ファイル
ビルド
- サービスとアプリケーションのsrcディレクトリ以下に入れてビルド
- Android SDK tools generate an IBinder interface based on the .aidl file and save it in the project's gen/ directory.
- The service must implement the IBinder interface as appropriate.
例
- 構文はJava
// IRemoteService.aidl package com.example.android; // Declare any non-default types here with import statements /** Example service interface */ interface IRemoteService { /** Request the process ID of this service, to do evil things with it. */ int getPid(); /** 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); }
メモ
- an implementation of an AIDL interface must be completely thread-safe.
AIDLを使用したIPCの実装
.aidlファイルを作成する
インターフェイスのメソッドを実装する
- AIDLコンパイラはAIDLのインターフェイスから Java プログラミング言語のインターフェイスを作成します。
- 子のインターフェイスには、このインターフェイスを継承した Stub という名前の内部抽象クラスがあります ( それと IPC 呼び出しに必要な若干の追加メソッドを実装しています ) 。
- 開発者は、YourInterface?.Stub を拡張したクラスを作成し、.aidl ファイルで宣言したメソッドを実装する必要があります。
- どこかで
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() { public int getPid(){ return Process.myPid(); } public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) { // Does nothing } };
- とか書いて実装。
- Android Studio/補完機能
- クラス名まで書いて右クリ→Generate...で(Alt+Insert)でオーバーライドの部分を生成できる
クライアントにインターフェイスを公開する
- サービスを作成している場合は、作成したインターフェイスの実装クラスのインスタンスを返却するために、Service を拡張し、Service.onBind(Intent) をオーバーライドする必要があります。
public class RemoteService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { // Return the interface return mBinder; } private final IRemoteService.Stub mBinder = new IRemoteService.Stub() { public int getPid(){ return Process.myPid(); } public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) { // Does nothing } }; }
android.os.Parcel
- カーネルを経由してプロセス間で受け渡しされるデータを抽象化したクラス
ParcelとAIDLで扱えるデータ型
データ型
- Java 言語のプリミティブ型 ( int、boolean、その他 )
- String
- List - java.util.ArrayList
- Map? - java.util.HashMap
- CharSequence
- その他 AIDL より生成されたインターフェイス
- Parcelableプロトコル を実装し、値で渡されるカスタムクラス
Android Studio/プロジェクト
- File->New->AIDL->AIDL File
参考
連載
- http://www.atmarkit.co.jp/ait/articles/1204/20/news140.html
- http://www.atmarkit.co.jp/ait/articles/1206/15/news124.html