1.バインダーの意味
- 直感的な視点:IBinderインターフェースを実装するAndroidのクラス
- IPCの視点:Androidのプロセス間通信方法
- 仮想物理デバイス。デバイスドライバーは/ dev / binderです。
- Androidフレームワークの観点:ServiceManagerは、さまざまなマネージャー(ActivityManager、WindowManagerなど)と対応するManagerServiceブリッジを接続します
- アプリケーション層の観点:クライアントとサーバー間の通信媒体です。bindServiceの場合、サーバーはサーバーのサービス呼び出しを含むBinderオブジェクトを変換します。このBindオブジェクトを介して、クライアントはサーバーが提供するサービスを取得できます。 。またはデータ。
2.バインダーの使用
バインダーは主にサービスで使用されます。通常のサービスのバインダーは、比較的単純でバインダーのコアに触れることができないプロセス間通信を必要としません。したがって、AIDLは、バインダーの動作メカニズムを分析するための例として使用されます。
最初にASでプロジェクトを作成し、次の3つのファイルを作成します:Book.java、Book.aidl、IBookManager.aidl
//Book.java文件
package com.syy.note1;
import android.os.Parcel;
import android.os.Parcelable;
public class Book implements Parcelable {
public int bookId;
public String bookName;
public Book (int bookId, String bookName) {
this.bookId = bookId;
this.bookName = bookName;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.bookId);
dest.writeString(this.bookName);
}
protected Book(Parcel in) {
this.bookId= in.readInt();
this.bookName= in.readString();
}
public static final Creator<Book> CREATOR = new Creator<Book>() {
@Override
public Book createFromParcel(Parcel source) {
return new Book(source);
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
}
//Book.aidl文件
package com.syy.note1;
parcelable Book;
// IBookManager.aidl文件
package com.syy.note1;
import com.syy.note1.Book;
// Declare any non-default types here with import statements
interface IBookManager {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
List<Book> getBookList();
void addBook(in Book book);
}
コンパイル後、システムが/ app / build / generate / aidl_source_output_dir / debug / out / package name / pathの下に対応するJavaファイルを自動的に生成したことがわかります。
/*
* This file is auto-generated. DO NOT MODIFY.
*/
package com.syy.note1;
// Declare any non-default types here with import statements
public interface IBookManager extends android.os.IInterface
{
/** Default implementation for IBookManager. */
public static class Default implements com.syy.note1.IBookManager
{
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
@Override public java.util.List<com.syy.note1.Book> getBookList() throws android.os.RemoteException
{
return null;
}
@Override public void addBook(com.syy.note1.Book book) 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.syy.note1.IBookManager
{
private static final java.lang.String DESCRIPTOR = "com.syy.note1.IBookManager";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.syy.note1.IBookManager interface,
* generating a proxy if needed.
*/
public static com.syy.note1.IBookManager asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.syy.note1.IBookManager))) {
return ((com.syy.note1.IBookManager)iin);
}
return new com.syy.note1.IBookManager.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;
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(descriptor);
return true;
}
case TRANSACTION_getBookList:
{
data.enforceInterface(descriptor);
java.util.List<com.syy.note1.Book> _result = this.getBookList();
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
case TRANSACTION_addBook:
{
data.enforceInterface(descriptor);
com.syy.note1.Book _arg0;
if ((0!=data.readInt())) {
_arg0 = com.syy.note1.Book.CREATOR.createFromParcel(data);
}
else {
_arg0 = null;
}
this.addBook(_arg0);
reply.writeNoException();
return true;
}
default:
{
return super.onTransact(code, data, reply, flags);
}
}
}
private static class Proxy implements com.syy.note1.IBookManager
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
@Override public java.util.List<com.syy.note1.Book> getBookList() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.util.List<com.syy.note1.Book> _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
boolean _status = mRemote.transact(Stub.TRANSACTION_getBookList, _data, _reply, 0);
if (!_status && getDefaultImpl() != null) {
return getDefaultImpl().getBookList();
}
_reply.readException();
_result = _reply.createTypedArrayList(com.syy.note1.Book.CREATOR);
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public void addBook(com.syy.note1.Book book) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
if ((book!=null)) {
_data.writeInt(1);
book.writeToParcel(_data, 0);
}
else {
_data.writeInt(0);
}
boolean _status = mRemote.transact(Stub.TRANSACTION_addBook, _data, _reply, 0);
if (!_status && getDefaultImpl() != null) {
getDefaultImpl().addBook(book);
return;
}
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
public static com.syy.note1.IBookManager sDefaultImpl;
}
static final int TRANSACTION_getBookList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_addBook = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
public static boolean setDefaultImpl(com.syy.note1.IBookManager impl) {
// Only one user of this interface can use this function
// at a time. This is a heuristic to detect if two different
// users in the same process use this function.
if (Stub.Proxy.sDefaultImpl != null) {
throw new IllegalStateException("setDefaultImpl() called twice");
}
if (impl != null) {
Stub.Proxy.sDefaultImpl = impl;
return true;
}
return false;
}
public static com.syy.note1.IBookManager getDefaultImpl() {
return Stub.Proxy.sDefaultImpl;
}
}
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
public java.util.List<com.syy.note1.Book> getBookList() throws android.os.RemoteException;
public void addBook(com.syy.note1.Book book) throws android.os.RemoteException;
}
自動生成されたファイルは乱雑に見えますが、実際、よく見ると構造が複雑ではなく、主に次の3つの部分で構成されていることがわかります。
-
スタブクラス:バインダークラスです
-
プロキシ:スタブの内部プロキシ
-
以前に定義したaidlのメソッド、および各メソッドのID
ステップバイステップの分析:
ディスクリプタ
バインダーの一意の識別子、通常は現在のクラス名
private static final java.lang.String DESCRIPTOR = "com.syy.note.IBookManager";
asInterface(android.os.IBinder obj)
これは、サーバーのBinderオブジェクトを、クライアントが必要とするAIDLインターフェイスタイプのオブジェクトに変換するために使用されます。クライアントとサーバーが同じプロセスにある場合は、サーバーのスタブオブジェクト自体が返され、異なる場合は返されます。プロセスでは、システムでカプセル化されたスタブが返されます。プロキシオブジェクト:
public static com.syy.note1.IBookManager asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.syy.note1.IBookManager))) {
return ((com.syy.note1.IBookManager)iin);
}
return new com.syy.note1.IBookManager.Stub.Proxy(obj);
}
onTransact(intコード、android.os.Parcelデータ、android.os.Parcel応答、intフラグ)
このメソッドは、サーバーのバインダースレッドプールで実行されます。クライアントがクロスプロセス要求を開始すると、リモート要求はシステムの最下層によってカプセル化され、処理のためにこのメソッドに渡されます。コードフィールドは呼び出されるメソッドを識別し、データはターゲットメソッドに必要なパラメーターであり、戻り値は応答に書き込まれます。このメソッドがfalseを返す場合、クライアント要求は失敗します。この機能を許可の検証に使用できます。結局のところ、どのプロセスもリモートでサービスを呼び出せないようにする必要があります。
バインダー作動機構