android中的AIDL进程间通信
一、一個(gè)android中AIDL的簡(jiǎn)單例子
?假如是這樣,現(xiàn)在有一個(gè)項(xiàng)目中提供了比較成熟的計(jì)算的方法,而現(xiàn)在我想開(kāi)發(fā)一款軟件其中一個(gè)模塊想用到一個(gè)計(jì)算類,而我又不想重新寫(xiě)了,那么就可以通過(guò)AIDL實(shí)現(xiàn)啦。假設(shè),已經(jīng)開(kāi)發(fā)完成的那個(gè)已經(jīng)提供了比較成熟的計(jì)算類的程序叫AIDLCalculateDemoServer(相當(dāng)于服務(wù)器),而我要寫(xiě)的程序叫AIDLCalculateDemoClient(相當(dāng)于客戶端),類似與客戶端服務(wù)器模式。首先至關(guān)的看下工程結(jié)構(gòu)圖:
? ? ? ? ? ? ? ? ??? ? ??? ? ? ?
?
圖1-1 服務(wù)器 圖1-2 客戶端
現(xiàn)在假設(shè)自己寫(xiě)的程序要調(diào)用服務(wù)端的運(yùn)算界面,輸入num1和num2,進(jìn)行遠(yuǎn)程運(yùn)算,調(diào)用服務(wù)端的接口,服務(wù)端運(yùn)算好之后,返回結(jié)果給客戶端,效果圖如下:
??? ? ??
然后來(lái)看看實(shí)現(xiàn),首先需要定義AIDL接口,客戶端和服務(wù)器端都要定義,并且要在同一包中,也就是圖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 }編譯發(fā)現(xiàn),目錄結(jié)構(gòu)如圖1-1和圖1-2中g(shù)en/com.example.aidl.calculate中多了CalculateInterface.java文件,內(nèi)容如下:
1 package com.example.aidl.calculate; 2 3 interface CalculateInterface { 4 double doCalculate(double a, double b); 5 }定義好接口就是要看服務(wù)端和客戶端的代碼啦,其中服務(wù)端主要看CalculateService代碼,這個(gè)一個(gè)繼承Service的類,在其中對(duì)AIDL中的接口進(jìn)行賦予實(shí)際意義,如下:
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", "遠(yuǎn)程計(jì)算中"); 61 Calculate calculate = new Calculate(); 62 double answer = calculate.calculateSum(a, b); 63 return answer; 64 } 65 }; 66 }然后可以看看,關(guān)鍵的服務(wù)都提供完畢,那么在客戶端是怎么訪問(wèn)的呢,要進(jìn)行綁定服務(wù)和一個(gè)ServiceConnection類完成,如下:
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("開(kāi)始遠(yuǎn)程運(yùn)算"); 76 try { 77 double num1 = Double.parseDouble(etNum1.getText().toString()); 78 double num2 = Double.parseDouble(etNum2.getText().toString()); 79 String answer = "計(jì)算結(jié)果:" + 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 }如此一來(lái),大功已經(jīng)基本告成,最后,我們?cè)趤?lái)看看服務(wù)端的配置文件吧:
<?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-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid: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>?
二、寫(xiě)AIDL注意事項(xiàng)
1. 客戶端和服務(wù)端的AIDL接口文件所在的包必須相同
2. 需要一個(gè)Service類的配合
?
?
學(xué)習(xí)中的一點(diǎn)總結(jié),歡迎拍磚哦^^
?
總結(jié)
以上是生活随笔為你收集整理的android中的AIDL进程间通信的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android View系统解析(上)
- 下一篇: Android中的Selector的用法