關於IPC應該不用多介紹了,Android系統中的進程之間不能共享內存,那麼若是兩個不一樣的應用程序之間須要通信怎麼辦呢?好比公司的一個項目要更新,產品的需求是依附於當前項目開發一個插件,可是呢這個插件功能以及界面比較複雜,不能和當前項目在一個進程中,同時呢,還要用到當前項目中已經寫好了的一些東西,那麼由於新開發的依附於當前項目的插件和當前項目不是一個進程,所以不能共享內存,就出現了問題,因而,須要提供一些機制在不一樣進程之間進行數據通訊,這個機制就是AIDL了。java
1、一個android中AIDL的簡單例子android
假如是這樣,如今有一個項目中提供了比較成熟的計算的方法,而如今我想開發一款軟件其中一個模塊想用到一個計算類,而我又不想從新寫了,那麼就能夠經過AIDL實現啦。假設,已經開發完成的那個已經提供了比較成熟的計算類的程序叫AIDLCalculateDemoServer(至關於服務器),而我要寫的程序叫AIDLCalculateDemoClient(至關於客戶端),相似與客戶端服務器模式。首先相當的看下工程結構圖:服務器
圖1-1 服務器 圖1-2 客戶端 app
如今假設本身寫的程序要調用服務端的運算界面,輸入num1和num2,進行遠程運算,調用服務端的接口,服務端運算好以後,返回結果給客戶端,效果圖以下:ide
而後來看看實現,首先須要定義AIDL接口,客戶端和服務器端都要定義,而且要在同一包中,也就是圖1-1和圖1-2 com.example.aidl.calculate中的CalculateInterface,其中的代碼以下:學習
1 package com.example.aidl.calculate; 2 3 interface CalculateInterface { 4 double doCalculate(double a, double b); 5 }
編譯發現,目錄結構如圖1-1和圖1-2中gen/com.example.aidl.calculate中多了CalculateInterface.java文件,內容以下:spa
1 package com.example.aidl.calculate; 2 3 interface CalculateInterface { 4 double doCalculate(double a, double b); 5 }
定義好接口就是要看服務端和客戶端的代碼啦,其中服務端主要看CalculateService代碼,這個一個繼承Service的類,在其中對AIDL中的接口進行賦予實際意義,以下:插件
1 package com.example.calculate; 2 3 import com.example.aidl.calculate.CalculateInterface; 4 import com.example.aidl.calculate.CalculateInterface.Stub; 5 6 import android.app.Service; 7 import android.content.Intent; 8 import android.os.IBinder; 9 import android.os.RemoteException; 10 import android.util.Log; 11 12 public class CalculateService extends Service { 13 14 private static final String TAG = "CalculateService"; 15 16 @Override 17 public IBinder onBind(Intent arg0) { 18 // TODO Auto-generated method stub 19 logE("onBind()"); 20 return mBinder; 21 } 22 23 @Override 24 public void onCreate() { 25 // TODO Auto-generated method stub 26 logE("onCreate()"); 27 super.onCreate(); 28 } 29 30 @Override 31 public void onStart(Intent intent, int startId) { 32 // TODO Auto-generated method stub 33 logE("onStart()"); 34 super.onStart(intent, startId); 35 } 36 37 @Override 38 public boolean onUnbind(Intent intent) { 39 // TODO Auto-generated method stub 40 logE("onUnbind()"); 41 return super.onUnbind(intent); 42 } 43 44 @Override 45 public void onDestroy() { 46 // TODO Auto-generated method stub 47 logE("onDestroy()"); 48 super.onDestroy(); 49 } 50 51 private static void logE(String str) { 52 Log.e(TAG, "--------" + str + "--------"); 53 } 54 55 private final CalculateInterface.Stub mBinder = new CalculateInterface.Stub() { 56 57 @Override 58 public double doCalculate(double a, double b) throws RemoteException { 59 // TODO Auto-generated method stub 60 Log.e("Calculate", "遠程計算中"); 61 Calculate calculate = new Calculate(); 62 double answer = calculate.calculateSum(a, b); 63 return answer; 64 } 65 }; 66 }
而後能夠看看,關鍵的服務都提供完畢,那麼在客戶端是怎麼訪問的呢,要進行綁定服務和一個ServiceConnection類完成,以下:code
1 package com.example.calculate; 2 3 import android.app.Activity; 4 import android.content.ComponentName; 5 import android.content.Context; 6 import android.content.Intent; 7 import android.content.ServiceConnection; 8 import android.graphics.Color; 9 import android.os.Bundle; 10 import android.os.IBinder; 11 import android.os.RemoteException; 12 import android.util.Log; 13 import android.view.View; 14 import android.widget.Button; 15 import android.widget.EditText; 16 import android.widget.TextView; 17 18 import com.example.aidl.calculate.CalculateInterface; 19 import com.example.aidlcalculatedemoclient.R; 20 21 public class CalculateClient extends Activity { 22 private static final String TAG = "CalculateClient"; 23 24 private Button btnCalculate; 25 26 private EditText etNum1; 27 28 private EditText etNum2; 29 30 private TextView tvResult; 31 32 private CalculateInterface mService; 33 34 private ServiceConnection mServiceConnection = new ServiceConnection() { 35 36 @Override 37 public void onServiceDisconnected(ComponentName name) { 38 // TODO Auto-generated method stub 39 logE("disconnect service"); 40 mService = null; 41 } 42 43 @Override 44 public void onServiceConnected(ComponentName name, IBinder service) { 45 // TODO Auto-generated method stub 46 logE("connect service"); 47 mService = CalculateInterface.Stub.asInterface(service); 48 } 49 }; 50 51 @Override 52 protected void onCreate(Bundle savedInstanceState) { 53 // TODO Auto-generated method stub 54 super.onCreate(savedInstanceState); 55 setContentView(R.layout.main); 56 57 Bundle args = new Bundle(); 58 Intent intent = new Intent("com.example.calculate.CalculateService"); 59 intent.putExtras(args); 60 bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE); 61 62 etNum1 = (EditText) findViewById(R.id.et_num_one); 63 etNum2 = (EditText) findViewById(R.id.et_num_two); 64 65 tvResult = (TextView) findViewById(R.id.tv_result); 66 67 btnCalculate = (Button) findViewById(R.id.btn_cal); 68 69 btnCalculate.setOnClickListener(new View.OnClickListener() { 70 71 @Override 72 public void onClick(View v) { 73 // TODO Auto-generated method stub 74 75 logE("開始遠程運算"); 76 try { 77 double num1 = Double.parseDouble(etNum1.getText().toString()); 78 double num2 = Double.parseDouble(etNum2.getText().toString()); 79 String answer = "計算結果:" + mService.doCalculate(num1, num2); 80 tvResult.setTextColor(Color.BLUE); 81 tvResult.setText(answer); 82 83 } catch (RemoteException e) { 84 } 85 } 86 }); 87 } 88 89 private void logE(String str) { 90 Log.e(TAG, "--------" + str + "--------"); 91 } 92 }
如此一來,大功已經基本告成,最後,咱們在來看看服務端的配置文件吧:server
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.aidlcaculatedemoserver" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.aidlcaculatedemoserver.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.example.calculate.CalculateService"> <intent-filter> <action android:name="com.example.calculate.CalculateService" /> </intent-filter> </service> </application> </manifest>
2、寫AIDL注意事項
1. 客戶端和服務端的AIDL接口文件所在的包必須相同
2. 須要一個Service類的配合
學習中的一點總結,歡迎拍磚哦^^