android中的AIDL處理序間通訊樣本_Android

來源:互聯網
上載者:User

關於IPC應該不用多介紹了,Android系統中的進程之間不能共用記憶體,那麼如果兩個不同的應用程式之間需要通訊怎麼辦呢?比如公司的一個項目要更新,產品的需求是依附於當前項目開發一個外掛程式,但是呢這個外掛程式功能以及介面比較複雜,不能和當前項目在一個進程中,同時呢,還要用到當前項目中已經寫好了的一些東西,那麼因為新開發的依附於當前項目的外掛程式和當前項目不是一個進程,因此不能共用記憶體,就出現了問題,於是,需要提供一些機制在不同進程之間進行資料通訊,這個機制就是AIDL了。

一、一個android中AIDL的簡單例子

假如是這樣,現在有一個項目中提供了比較成熟的計算的方法,而現在我想開發一款軟體其中一個模組想用到一個計算類,而我又不想重新寫了,那麼就可以通過AIDL實現啦。假設,已經開發完成的那個已經提供了比較成熟的計算類的程式叫AIDLCalculateDemoServer(相當於伺服器),而我要寫的程式叫AIDLCalculateDemoClient(相當於用戶端),類似與用戶端伺服器模式。首先至關的看下工程結構圖:
                                     圖1-1 伺服器                                                       圖1-2 用戶端 

現在假設自己寫的程式要調用服務端的運算介面,輸入num1和num2,進行遠程運算,調用服務端的介面,服務端運算好之後,返回結果給用戶端,效果圖如下:
然後來看看實現,首先需要定義AIDL介面,用戶端和伺服器端都要定義,並且要在同一包中,也就是圖1-1和圖1-2 com.example.aidl.calculate中的CalculateInterface,其中的代碼如下:

package com.example.aidl.calculate;interface CalculateInterface {  double doCalculate(double a, double b); }

編譯發現,目錄結構如圖1-1和圖1-2中gen/com.example.aidl.calculate中多了CalculateInterface.java檔案,內容如下:

 package com.example.aidl.calculate; interface CalculateInterface {   double doCalculate(double a, double b); }

定義好介面就是要看服務端和用戶端的代碼啦,其中服務端主要看CalculateService代碼,這個一個繼承Service的類,在其中對AIDL中的介面進行賦予實際意義,如下:

 package com.example.calculate;import com.example.aidl.calculate.CalculateInterface;import com.example.aidl.calculate.CalculateInterface.Stub;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;public class CalculateService extends Service {    private static final String              TAG            =  "CalculateService";    @Override  public IBinder onBind(Intent arg0) {    // TODO Auto-generated method stub    logE("onBind()");    return mBinder;  }    @Override  public void onCreate() {    // TODO Auto-generated method stub    logE("onCreate()");    super.onCreate();  }    @Override  public void onStart(Intent intent, int startId) {    // TODO Auto-generated method stub    logE("onStart()");    super.onStart(intent, startId);  }    @Override  public boolean onUnbind(Intent intent) {    // TODO Auto-generated method stub    logE("onUnbind()");    return super.onUnbind(intent);  }    @Override  public void onDestroy() {    // TODO Auto-generated method stub    logE("onDestroy()");    super.onDestroy();  }    private static void logE(String str) {    Log.e(TAG, "--------" + str + "--------");  }    private final CalculateInterface.Stub mBinder = new CalculateInterface.Stub() {        @Override    public double doCalculate(double a, double b) throws RemoteException {      // TODO Auto-generated method stub      Log.e("Calculate", "遠程計算中");      Calculate calculate = new Calculate();      double answer = calculate.calculateSum(a, b);      return answer;    }  };}

然後可以看看,關鍵的服務都提供完畢,那麼在用戶端是怎麼訪問的呢,要進行綁定服務和一個ServiceConnection類完成,如下:

package com.example.calculate;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.graphics.Color;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import com.example.aidl.calculate.CalculateInterface;import com.example.aidlcalculatedemoclient.R;public class CalculateClient extends Activity {  private static final String         TAG            =      "CalculateClient";    private       Button         btnCalculate;    private       EditText        etNum1;    private         EditText        etNum2;    private       TextView        tvResult;    private        CalculateInterface   mService;    private       ServiceConnection    mServiceConnection = new ServiceConnection() {        @Override    public void onServiceDisconnected(ComponentName name) {      // TODO Auto-generated method stub      logE("disconnect service");      mService = null;    }        @Override    public void onServiceConnected(ComponentName name, IBinder service) {      // TODO Auto-generated method stub      logE("connect service");      mService = CalculateInterface.Stub.asInterface(service);    }  };    @Override  protected void onCreate(Bundle savedInstanceState) {    // TODO Auto-generated method stub    super.onCreate(savedInstanceState);    setContentView(R.layout.main);        Bundle args = new Bundle();    Intent intent = new Intent("com.example.calculate.CalculateService");    intent.putExtras(args);    bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);        etNum1 = (EditText) findViewById(R.id.et_num_one);    etNum2 = (EditText) findViewById(R.id.et_num_two);        tvResult = (TextView) findViewById(R.id.tv_result);        btnCalculate = (Button) findViewById(R.id.btn_cal);        btnCalculate.setOnClickListener(new View.OnClickListener() {            @Override      public void onClick(View v) {        // TODO Auto-generated method stub                logE("開始遠程運算");        try {          double num1 = Double.parseDouble(etNum1.getText().toString());          double num2 = Double.parseDouble(etNum2.getText().toString());          String answer = "計算結果:" + mService.doCalculate(num1, num2);          tvResult.setTextColor(Color.BLUE);          tvResult.setText(answer);                      } catch (RemoteException e) {        }      }    });  }    private void logE(String str) {    Log.e(TAG, "--------" + str + "--------");  }}

如此一來,大功已經基本告成,最後,我們在來看看服務端的設定檔吧:

<?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>

二、寫AIDL注意事項

1. 用戶端和服務端的AIDL介面檔案所在的包必須相同

2. 需要一個Service類的配合

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.