欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android中的AIDL進程間通信示例

 更新時間:2016年11月05日 15:50:04   作者:VincentCZW  
進程之間不能共享內(nèi)存,那么怎么在不同的應(yīng)用程序中進行通訊,這就要依賴AIDL機制,本文詳細介紹了android中的AIDL進程間通信示例,有興趣的可以了解一下。

關(guān)于IPC應(yīng)該不用多介紹了,Android系統(tǒng)中的進程之間不能共享內(nèi)存,那么如果兩個不同的應(yīng)用程序之間需要通訊怎么辦呢?比如公司的一個項目要更新,產(chǎn)品的需求是依附于當前項目開發(fā)一個插件,但是呢這個插件功能以及界面比較復(fù)雜,不能和當前項目在一個進程中,同時呢,還要用到當前項目中已經(jīng)寫好了的一些東西,那么因為新開發(fā)的依附于當前項目的插件和當前項目不是一個進程,因此不能共享內(nèi)存,就出現(xiàn)了問題,于是,需要提供一些機制在不同進程之間進行數(shù)據(jù)通信,這個機制就是AIDL了。

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

假如是這樣,現(xiàn)在有一個項目中提供了比較成熟的計算的方法,而現(xiàn)在我想開發(fā)一款軟件其中一個模塊想用到一個計算類,而我又不想重新寫了,那么就可以通過AIDL實現(xiàn)啦。假設(shè),已經(jīng)開發(fā)完成的那個已經(jīng)提供了比較成熟的計算類的程序叫AIDLCalculateDemoServer(相當于服務(wù)器),而我要寫的程序叫AIDLCalculateDemoClient(相當于客戶端),類似與客戶端服務(wù)器模式。首先至關(guān)的看下工程結(jié)構(gòu)圖:
                                     圖1-1 服務(wù)器                                                       圖1-2 客戶端 

現(xiàn)在假設(shè)自己寫的程序要調(diào)用服務(wù)端的運算界面,輸入num1和num2,進行遠程運算,調(diào)用服務(wù)端的接口,服務(wù)端運算好之后,返回結(jié)果給客戶端,效果圖如下:
然后來看看實現(xiàn),首先需要定義AIDL接口,客戶端和服務(wù)器端都要定義,并且要在同一包中,也就是圖1-1和圖1-2 com.example.aidl.calculate中的CalculateInterface,其中的代碼如下:

package com.example.aidl.calculate;

interface CalculateInterface {
  double doCalculate(double a, double b);
 }

編譯發(fā)現(xiàn),目錄結(jié)構(gòu)如圖1-1和圖1-2中g(shù)en/com.example.aidl.calculate中多了CalculateInterface.java文件,內(nèi)容如下:

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

定義好接口就是要看服務(wù)端和客戶端的代碼啦,其中服務(wù)端主要看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;
    }
  };
}

然后可以看看,關(guān)鍵的服務(wù)都提供完畢,那么在客戶端是怎么訪問的呢,要進行綁定服務(wù)和一個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 = "計算結(jié)果:" + mService.doCalculate(num1, num2);
          tvResult.setTextColor(Color.BLUE);
          tvResult.setText(answer);
              
        } catch (RemoteException e) {
        }
      }
    });
  }
  
  private void logE(String str) {
    Log.e(TAG, "--------" + str + "--------");
  }
}

如此一來,大功已經(jīng)基本告成,最后,我們在來看看服務(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-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. 客戶端和服務(wù)端的AIDL接口文件所在的包必須相同

2. 需要一個Service類的配合

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android抓取CSDN首頁極客頭條內(nèi)容完整實例

    Android抓取CSDN首頁極客頭條內(nèi)容完整實例

    這篇文章主要介紹了Android抓取CSDN首頁極客頭條內(nèi)容完整實例,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Android7.0自動更新適配 包解析異常

    Android7.0自動更新適配 包解析異常

    這篇文章主要為大家詳細介紹了Android7.0自動更新適配,包解析異常的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android Bitmap和Drawable相互轉(zhuǎn)換的簡單代碼

    Android Bitmap和Drawable相互轉(zhuǎn)換的簡單代碼

    Android Bitmap和Drawable相互轉(zhuǎn)換的簡單代碼,需要的朋友可以參考一下
    2013-05-05
  • Android微信第三方登錄(個人筆記)

    Android微信第三方登錄(個人筆記)

    這篇文章主要為大家詳細介紹了Android微信第三方登錄的具體過程,個人筆記分享,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android Studio4.0解決Gradle下載超時問題

    Android Studio4.0解決Gradle下載超時問題

    這篇文章主要介紹了Android Studio4.0解決Gradle下載超時問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Android NDK 開發(fā)中 SO 包大小壓縮方法詳解

    Android NDK 開發(fā)中 SO 包大小壓縮方法詳解

    這篇文章主要為為大家介紹了Android NDK 開發(fā)中 SO 包大小壓縮方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Android?Java?crash?處理流程詳解

    Android?Java?crash?處理流程詳解

    這篇文章主要為大家介紹了Android?Java?crash?處理流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Android編程向服務(wù)器發(fā)送請求時出現(xiàn)中文亂碼問題的解決方法

    Android編程向服務(wù)器發(fā)送請求時出現(xiàn)中文亂碼問題的解決方法

    這篇文章主要介紹了Android編程向服務(wù)器發(fā)送請求時出現(xiàn)中文亂碼問題的解決方法,實例分析了Android參數(shù)傳遞過程中中文亂碼的解決技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android 自定義狀態(tài)欄實例代碼

    Android 自定義狀態(tài)欄實例代碼

    本文通過實例代碼給大家講解了Android 自定義狀態(tài)欄知識,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2017-02-02
  • Kotlin協(xié)程的啟動方式介紹

    Kotlin協(xié)程的啟動方式介紹

    這篇文章我們來講協(xié)程的啟動,畢竟協(xié)程是一個很強大的設(shè)計模式,深入了解需要花很多的時間,我們先從簡單開始,其實學(xué)會了簡單的使用,基本已經(jīng)可以滿足我們平時的開發(fā)需要了,話不多說,開始
    2022-09-09

最新評論