aidlノート

9119 ワード

aidlノート、
aidlファイルコードは次のとおりです.
package com.zhy.calc.aidl;
interface ICalcAIDL
{
	int add(int x , int y);
	int min(int x , int y );
}

生成されたjavaファイルコードは以下の通りです(編集して、参考にしてください):
package com.example.zhy_binder;

public interface ICalcAIDL extends android.os.IInterface {
	public static abstract class Stub extends android.os.Binder implements com.zhy.calc.aidl.ICalcAIDL {
		private static final java.lang.String DESCRIPTOR = "com.zhy.calc.aidl.ICalcAIDL";

		public Stub() 
		{
			this.attachInterface(this, DESCRIPTOR);
		}

		public static com.zhy.calc.aidl.ICalcAIDL asInterface(android.os.IBinder obj) 
		{
			if ((obj == null)) 
			{
				return null;
			}
			android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
			if (((iin != null) && (iin instanceof com.zhy.calc.aidl.ICalcAIDL))) 
			{
				return ((com.zhy.calc.aidl.ICalcAIDL) iin);
			}
			return new com.zhy.calc.aidl.ICalcAIDL.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 {
			switch (code) 
			{
				case INTERFACE_TRANSACTION: 
				{
					reply.writeString(DESCRIPTOR);
					return true;
				}
				case TRANSACTION_add: 
				{
					data.enforceInterface(DESCRIPTOR);
					int _arg0;
					_arg0 = data.readInt();
					int _arg1;
					_arg1 = data.readInt();
					int _result = this.add(_arg0, _arg1);
					reply.writeNoException();
					reply.writeInt(_result);
					return true;
				}
				case TRANSACTION_min: 
				{
					data.enforceInterface(DESCRIPTOR);
					int _arg0;
					_arg0 = data.readInt();
					int _arg1;
					_arg1 = data.readInt();
					int _result = this.min(_arg0, _arg1);
					reply.writeNoException();
					reply.writeInt(_result);
					return true;
				}
			}
			return super.onTransact(code, data, reply, flags);
		}

		private static class Proxy implements com.zhy.calc.aidl.ICalcAIDL {
			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;
			}

			@Override
			public int add(int x, int y) throws android.os.RemoteException {
				android.os.Parcel _data = android.os.Parcel.obtain();
				android.os.Parcel _reply = android.os.Parcel.obtain();
				int _result;
				try {
					_data.writeInterfaceToken(DESCRIPTOR);
					_data.writeInt(x);
					_data.writeInt(y);
					mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
					_reply.readException();
					_result = _reply.readInt();
				} finally {
					_reply.recycle();
					_data.recycle();
				}
				return _result;
			}

			@Override
			public int min(int x, int y) throws android.os.RemoteException {
				android.os.Parcel _data = android.os.Parcel.obtain();
				android.os.Parcel _reply = android.os.Parcel.obtain();
				int _result;
				try {
					_data.writeInterfaceToken(DESCRIPTOR);
					_data.writeInt(x);
					_data.writeInt(y);
					mRemote.transact(Stub.TRANSACTION_min, _data, _reply, 0);
					_reply.readException();
					_result = _reply.readInt();
				} finally {
					_reply.recycle();
					_data.recycle();
				}
				return _result;
			}
		}

		static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
		static final int TRANSACTION_min = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
	}

	public int add(int x, int y) throws android.os.RemoteException;

	public int min(int x, int y) throws android.os.RemoteException;
}
以上がプロジェクトにおけるaidlファイルの自動生成である.JAvaファイル.このjavaファイルICalcAIDLには、抽象クラスStubと、2つの実装されていないメソッドadd,minが含まれています.いずれも抽象的であるため、ICalcAIDLをinterfaceとして定義します.
その内部の抽象クラスStub,extendsはBinderクラスでありimplementsはその外部クラスcomである.zhy.calc.aidl.ICalcAIDL.
内部クラスStubには内部クラスProxyが定義、外部クラスcomもimplementsに定義されている.zhy.calc.aidl.ICalcAIDL.
クライアントのコードは次のとおりです.
package com.example.zhy_binder_client;

import android.app.Activity;
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.util.Log;
import android.view.View;
import android.widget.Toast;

import com.zhy.calc.aidl.ICalcAIDL;

public class MainActivity extends Activity
{
	private ICalcAIDL mCalcAidl;

	private ServiceConnection mServiceConn = new ServiceConnection()
	{
		@Override
		public void onServiceDisconnected(ComponentName name)
		{
			Log.e("client", "onServiceDisconnected");
			mCalcAidl = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service)
		{
			Log.e("client", "onServiceConnected");
			//
			mCalcAidl = ICalcAIDL.Stub.asInterface(service);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}
	
	/**
	 *  BindService 
	 * @param view
	 */
	public void bindService(View view)
	{
		Intent intent = new Intent();
		intent.setAction("com.zhy.aidl.calc");
		bindService(intent, mServiceConn, Context.BIND_AUTO_CREATE);
	}
	/**
	 *  unBindService 
	 * @param view
	 */
	public void unbindService(View view)
	{
		unbindService(mServiceConn);
	}
	/**
	 *  12+12 
	 * @param view
	 */
	public void addInvoked(View view) throws Exception
	{

		if (mCalcAidl != null)
		{
			int addRes = mCalcAidl.add(12, 12);
			Toast.makeText(this, addRes + "", Toast.LENGTH_SHORT).show();
		} else
		{
			Toast.makeText(this, " , ", Toast.LENGTH_SHORT)
					.show();

		}

	}
	/**
	 *  50-12 
	 * @param view
	 */
	public void minInvoked(View view) throws Exception
	{

		if (mCalcAidl != null)
		{
			int addRes = mCalcAidl.min(50, 12);
			Toast.makeText(this, addRes + "", Toast.LENGTH_SHORT).show();
		} else
		{
			Toast.makeText(this, " , ", Toast.LENGTH_SHORT)
					.show();

		}

	}

}

サーバ側のコードは次のとおりです.
package com.example.zhy_binder;

import com.zhy.calc.aidl.ICalcAIDL;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class CalcService extends Service
{
	private static final String TAG = "server";

	public void onCreate()
	{
		Log.e(TAG, "onCreate");
	}

	public IBinder onBind(Intent t)
	{
		Log.e(TAG, "onBind");
		return mBinder;
	}

	public void onDestroy()
	{
		Log.e(TAG, "onDestroy");
		super.onDestroy();
	}

	public boolean onUnbind(Intent intent)
	{
		Log.e(TAG, "onUnbind");
		return super.onUnbind(intent);
	}

	public void onRebind(Intent intent)
	{
		Log.e(TAG, "onRebind");
		super.onRebind(intent);
	}

	private final ICalcAIDL.Stub mBinder = new ICalcAIDL.Stub()
	{

		@Override
		public int add(int x, int y) throws RemoteException
		{
			return x + y;
		}

		@Override
		public int min(int x, int y) throws RemoteException
		{
			return x - y;
		}

	};

}

クライアントがサービス側を起動すると、サービス側はbinderオブジェクトを返し、クライアントはこのbinderオブジェクトを介してサービス側が提供する方法を呼び出す.
分析コードは次のとおりです.
サービス側のonBindメソッドでは、次のように44行のmBinderが返されます.
	private final ICalcAIDL.Stub mBinder = new ICalcAIDL.Stub()
	{

		@Override
		public int add(int x, int y) throws RemoteException
		{
			return x + y;
		}

		@Override
		public int min(int x, int y) throws RemoteException
		{
			return x - y;
		}

	};
このmBinderは抽象クラスICalcAIDLであることがわかる.Stubのオブジェクトは、Binderとimplementsの親を継承するので、mBinderは抽象クラスICalcAIDLを実装する必要があります.Stubの抽象的な方法addとmin.
クライアントのコード34行目は次のとおりです.
	mCalcAidl = ICalcAIDL.Stub.asInterface(service);
サービス側から戻ってきたbinderオブジェクトをProxyクラスの構造パラメータnewとしてProxyクラスのオブジェクトとした.そしてクライアントではこのProxyクラスのオブジェクトmCalcAidlを利用してメソッドaddまたはminを呼び出す.
このmCalcAidlによるメソッドaddの呼び出しは、クライアント73行において、int addRes=mCalcAidlである.add(12, 12); 実際に呼び出されたのは、生成ファイルの87~103行目で、以下のようになります.
			public int add(int x, int y) throws android.os.RemoteException {
				android.os.Parcel _data = android.os.Parcel.obtain();
				android.os.Parcel _reply = android.os.Parcel.obtain();
				int _result;
				try {
					_data.writeInterfaceToken(DESCRIPTOR);
					_data.writeInt(x);
					_data.writeInt(y);
					mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
					_reply.readException();
					_result = _reply.readInt();
				} finally {
					_reply.recycle();
					_data.recycle();
				}
				return _result;
			}

この9行目、つまりファイルを生成する95行目に注意してください.以下のようにします.
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
呼び出し者mRemoteは、上述したサービス側からProxy構築パラメータとしてbinderオブジェクトが渡され、このbinderはstubクラスのオブジェクトであるため、この行コードが実際に呼び出されるのはstubクラスにおけるonTransactメソッド、すなわちファイルを生成する34行目である.
参照先:http://blog.csdn.net/lmj623565791/article/details/38461079