#AIDL
---
1. 創建一個接口,再里面定義方法
```
package com.example.taidl;
interface ICalcAIDL
{
int add(int x , int y);
int min(int x , int y );
}
```
build一下gen目錄下會生成ICalcAIDL.java文件
```
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /Users/dream/Downloads/android/androidProject/TAIDL/src/com/example/taidl/ICalcAIDL.aidl
*/
package com.example.taidl;
public interface ICalcAIDL extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.example.taidl.ICalcAIDL
{
private static final java.lang.String DESCRIPTOR = "com.example.taidl.ICalcAIDL";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.taidl.ICalcAIDL interface,
* generating a proxy if needed.
*/
public static com.example.taidl.ICalcAIDL asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.taidl.ICalcAIDL))) {
return ((com.example.taidl.ICalcAIDL)iin);
}
return new com.example.taidl.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.example.taidl.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;
}
```
2. 新建一個Service
```
package com.example.taidl;
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 min(int x, int y) throws RemoteException {
return x + y;
}
@Override
public int add(int x, int y) throws RemoteException {
// TODO Auto-generated method stub
return x - y;
}
};
}
```
創建了一個mBinder對象,并在Service的onBind方法中返回
注冊:
```
<service android:name="com.example.taidl.CalcService">
<intent-filter>
<action android:name="com.example.taidl.calc" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
```
我們一會會在別的應用程序中通過Intent來查找此Service;這個不需要Activity,所以我也就沒寫Activity,安裝完成也看不到安裝圖標,悄悄在后臺運行著。服務端編寫完畢。下面開始編寫客戶端:
```
package com.example.tclient;
import com.example.taidl.ICalcAIDL;
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;
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.example.taidl.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();
}
}
}
```
將服務端的aidl文件完整的復制過來,包名一定要一致。
##分析AIDL生成的代碼
1. 服務端
```
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;
}
};
```
ICalcAILD.Stub來執行的,讓我們來看看Stub這個類的聲明:
```
public static abstract class Stub extends android.os.Binder implements com.zhy.calc.aidl.ICalcAIDL
```
清楚的看到這個類是Binder的子類,是不是符合我們文章開通所說的服務端其實是一個Binder類的實例
接下來看它的onTransact()方法:
```
@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);
}
```
文章開頭也說到服務端的Binder實例會根據客戶端依靠Binder驅動發來的消息,執行onTransact方法,然后由其參數決定執行服務端的代碼。
可以看到onTransact有四個參數
code , data ,replay , flags
* code 是一個整形的唯一標識,用于區分執行哪個方法,客戶端會傳遞此參數,告訴服務端執行哪個方法
* data客戶端傳遞過來的參數
* replay服務器返回回去的值
* flags標明是否有返回值,0為有(雙向),1為沒有(單向)
我們仔細看case TRANSACTION_min中的代碼
data.enforceInterface(DESCRIPTOR);
與客戶端的writeInterfaceToken對用,標識遠程服務的名稱
```
int _arg0;
_arg0 = data.readInt();
int _arg1;
_arg1 = data.readInt();
```
接下來分別讀取了客戶端傳入的兩個參數
```
int _result = this.min(_arg0, _arg1);
reply.writeNoException();
reply.writeInt(_result);
```
然后執行this.min,即我們實現的min方法;返回result由reply寫回。
add同理,可以看到服務端通過AIDL生成Stub的類,封裝了服務端本來需要寫的代碼。
###客戶端
客戶端主要通過ServiceConnected與服務端連接
```
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);
}
};
```
如果你比較敏銳,應該會猜到這個onServiceConnected中的IBinder實例,其實就是我們文章開通所說的Binder驅動,也是一個Binder實例
在ICalcAIDL.Stub.asInterface中最終調用了:
```
return new com.zhy.calc.aidl.ICalcAIDL.Stub.Proxy(obj);
```
這個Proxy實例傳入了我們的Binder驅動,并且封裝了我們調用服務端的代碼,文章開頭說,客戶端會通過Binder驅動的transact()方法調用服務端代碼
直接看Proxy中的add方法
```
@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;
}
```
首先聲明兩個Parcel對象,一個用于傳遞數據,一個用戶接收返回的數據
```
_data.writeInterfaceToken(DESCRIPTOR);與服務器端的enforceInterfac對應
_data.writeInt(x);
_data.writeInt(y);寫入需要傳遞的參數
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
```
終于看到了我們的transact方法,第一個對應服務端的code,_data,_repay分別對應服務端的data,reply,0表示是雙向的
```
_reply.readException();
_result = _reply.readInt();
```
最后讀出我們服務端返回的數據,然后return。可以看到和服務端的onTransact基本是一行一行對應的。
我們已經通過AIDL生成的代碼解釋了Android Binder框架的工作原理。Service的作用其實就是為我們創建Binder驅動,即服務端與客戶端連接的橋梁。
- JavaSE(Java基礎)
- Java基礎知識
- Java中的內存泄漏
- String源碼分析
- Java集合結構
- ArrayList源碼剖析
- HashMap源碼剖析
- Hashtable簡介
- Vector源碼剖析
- LinkedHashMap簡介
- LinkedList簡介
- JVM(Java虛擬機)
- JVM基礎知識
- JVM類加載機制
- Java內存區域與內存溢出
- 垃圾回收算法
- Java并發(JavaConcurrent)
- Java并發基礎知識
- 生產者和消費者問題
- Thread和Runnable實現多線程的區別
- 線程中斷
- 守護線程與阻塞線程的情況
- Synchronized
- 多線程環境中安全使用集合API
- 實現內存可見的兩種方法比較:加鎖和volatile變量
- 死鎖
- 可重入內置鎖
- 使用wait/notify/notifyAll實現線程間通信
- NIO
- 數據結構(DataStructure)
- 數組
- 棧和隊列
- Algorithm(算法)
- 排序
- 選擇排序
- 冒泡排序
- 快速排序
- 歸并排序
- 查找
- 順序查找
- 折半查找
- Network(網絡)
- TCP/UDP
- HTTP
- Socket
- OperatingSystem(操作系統)
- Linux系統的IPC
- android中常用設計模式
- 面向對象六大原則
- 單例模式
- Builder模式
- 原型模式
- 簡單工廠
- 策略模式
- 責任鏈模式
- 觀察者模式
- 代理模式
- 適配器模式
- 外觀模式
- Android(安卓面試點)
- Android基礎知識
- Android內存泄漏總結
- Handler內存泄漏分析及解決
- Android性能優化
- ListView詳解
- RecyclerView和ListView的異同
- AsyncTask源碼分析
- 插件化技術
- 自定義控件
- ANR問題
- Art和Dalvik的區別
- Android關于OOM的解決方案
- Fragment
- SurfaceView
- Android幾種進程
- APP啟動過程
- 圖片三級緩存
- Bitmap的分析與使用
- 熱修復的原理
- AIDL
- Binder機制
- Zygote和System進程的啟動過程
- Android中的MVC,MVP和MVVM
- MVP
- Android開機過程
- EventBus用法詳解
- 查漏補缺
- Git操作