Android AIDL(安卓接口定義語言)基本使用方法

跨進程間通信,分別有Client端和Service端不同進程

1.定義AIDL文件,此文件現代當於一個協議,定義服務端要實現的方法。
如:
  1. package com.example.aidl;  
  2. interface IMyService {  
  3.     void play();  
  4.     void pause();  
  5. }  

2.實現服務端Service:
如:
public class AIDLService extends Service {  
  1.     private MyBinder mBinder;  
  2.     @Override  
  3.     public IBinder onBind(Intent intent) { 
  4. mBinder = new MyBinder();
  5.         return mBinder;  //返回binder對象
  6.     }  
  7.      /*  
  8.      * 該類繼承了IMyService.Stub類而不是extends Binder類。 
  9.      * IMyService.Stub是Binder的子類。 
  10.      * 進程內的Service定義MyBinder內部類是繼承Binder類。 
  11.      */  
  12.     public class MyBinder extends IMyService.Stub {  
  13.         @Override  
  14.         public void play() throws RemoteException {  
  15.            Log.i(tag, "service 自定義 play()...");   
  16.         }  
  17.         @Override  
  18.         public void pause() throws RemoteException {  
  19.             Log.i(tag, "service 自定義 pause()..."); 
  20.         }  
  21.     }  

3.把AIDL文件接口複製到Client項目

4.在Client項目中調用Service項目的服務。
如:
  1. public class MainActivity extends Activity implements OnClickListener {  
  2.     Button btnBind, btnPlay, btnPause;  
  3.     IMyService mBinder; // 接口的一個引用  
  4.     boolean mIsBind = false// 綁定值爲true,未綁定製爲false;  
  5.     private ServiceConnection mConn = new ServiceConnection() {  
  6.         @Override  
  7.         public void onServiceDisconnected(ComponentName name) {  
  8.         }  
  9.         @Override  
  10.         public void onServiceConnected(ComponentName name, IBinder service) {  
  11.             /* 
  12.              * 獲得另一個進程中的Service傳遞過來的IBinder對象-service, 
  13.              * 用IMyService.Stub.asInterface方法轉換該對象,這點與進程內的通信不同 
  14.              */  
  15.             mBinder = IMyService.Stub.asInterface(service);  
  16.             mIsBind = true;  
  17.             Log.i("MainActivity""onServiceConnected....");  
  18.         }  
  19.     };   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.         btnBind = (Button) findViewById(R.id.btn_bind);  
  25.         btnPlay = (Button) findViewById(R.id.btn_play);  
  26.         btnPause = (Button) findViewById(R.id.btn_pause);  
  27.         btnBind.setOnClickListener(this);  
  28.         btnPlay.setOnClickListener(this);  
  29.         btnPause.setOnClickListener(this);  
  30.     }   
  31.     @Override  
  32.     public void onClick(View v) {  
  33.         Intent intent = new Intent();  
  34.         int btn = v.getId();  
  35.         switch (btn) {  
  36.         case R.id.btn_bind:  
  37.             intent.setAction(Constant.ACTION_AIDL);  
  38.             bindService(intent, mConn, BIND_AUTO_CREATE); //綁定服務
  39.             break;  
  40.         case R.id.btn_play:  
  41.             if (mIsBind){  
  42.                 try {  
  43.                     mBinder.play(); //調用IMyService接口的方法,服務端的邏輯就可以使用
  44.                 } catch (RemoteException e) {  
  45.                     e.printStackTrace();  
  46.                 }//調用service中的play()  
  47.             }  
  48.                 break;  
  49.         case R.id.btn_pause:  
  50.             if(mIsBind){  
  51.                 try {  
  52.                     mBinder.pause(); //調用IMyService接口的方法
  53.                 } catch (Exception e) {  
  54.                     e.printStackTrace();  
  55.                 }  
  56.             }  
  57.             break;  
  58.         }  
  59.     }  
  60. }  

以上可以看到,Stub是IMyService中的一個靜態抽象類,繼承了Binder,並且實現了IMyService接口。這也就解釋了我們定義IMyInterface.Stub的時候爲什麼需要實現IMyService中的方法了,也說明了爲什麼我們可以把IMyService.Stub向上轉型成IBinder了。

總結: 
AIDL的最終效果就是讓 IPC的通訊就像調用函數那樣簡單。自動的幫你完成了參數序列化發送以及解析返回數據的那一系列麻煩。而你所需要做的就是寫上一個接口文件,然後利用aidl工具轉化一下得到另一個java文件,這個文件在服務和客戶端程序各放一份。服務程序繼承IxxxxService.Stub 然後將函數接口裏面的邏輯代碼實現一下。 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章