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

Android AIDL實(shí)現(xiàn)進(jìn)程間通信探索

 更新時(shí)間:2016年09月23日 09:58:32   作者:總李寫代碼  
這篇文章主要為大家詳細(xì)介紹了Android AIDL實(shí)現(xiàn)進(jìn)程間通信的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言: 

     前面總結(jié)了程序間共享數(shù)據(jù),可以使用ContentProvider也可以使用SharedPreference,那么進(jìn)程間怎么共享內(nèi)存呢?Android系統(tǒng)中的進(jìn)程之間不能共享內(nèi)存,因此,需要提供一些機(jī)制在不同進(jìn)程之間進(jìn)行數(shù)據(jù)通信。 

     為了使其他的應(yīng)用程序也可以訪問本應(yīng)用程序提供的服務(wù),Android系統(tǒng)采用了遠(yuǎn)程過程調(diào)用(Remote Procedure Call,RPC)方式來實(shí)現(xiàn)。與很多其他的基于RPC的解決方案一樣,Android使用一種接口定義語言(Interface Definition Language,IDL)來公開服務(wù)的接口。我們知道4個(gè)Android應(yīng)用程序組件中的3個(gè)(Activity、BroadcastReceiver和ContentProvider)都可以進(jìn)行跨進(jìn)程訪問,另外一個(gè)Android應(yīng)用程序組件Service同樣可以。因此,可以將這種可以跨進(jìn)程訪問的服務(wù)稱為AIDL(Android Interface Definition Language)服務(wù)。 

接下來實(shí)戰(zhàn)一下具體實(shí)現(xiàn): 

 1.)首先新建一個(gè)aidl文件 

interface ITestInterface {
   //獲取進(jìn)程ID
  int getProcessId();
  //處理字符串
  String dealString( String srcString);
  //字符串追加
  String appendString( String srcString);

  void addPerson(in Person person);

  List<Person> getPersons();
} 

aidl語法解說:
 •  聲明函數(shù)基本和Java一致,可以傳參和返回值,參數(shù)和返回值
 •  參數(shù)和返回值   Java編程語言的基本數(shù)據(jù)類型 (int, long, char, boolean等),String和CharSequence,集合接口類型List和Map、其他AIDL接口類型、實(shí)現(xiàn)Parcelable接口的自定義對象
 •  方向指示    在使用aidl傳輸數(shù)據(jù)時(shí),對于非基本數(shù)據(jù)類型,也不是String和CharSequence類型的,(即Parcelable類型)需要有方向指示,包括in、out和inout。
 •  AIDL只支持接口方法,不能公開static變量。 

2.)服務(wù)端實(shí)現(xiàn)接口 

  private final ITestInterface.Stub mBinder = new ITestInterface.Stub() {
    public int getProcessId(){
      Log.e("TestService","TestService Thread: " + Thread.currentThread().getName());
      Log.e("TestService","TestService getProcessId()");
      return android.os.Process.myPid();
    }
    //處理字符串
    public String dealString( String srcString)
    {
      return srcString+srcString;
    }

    //字符串追加
    public String appendString( String srcString)
    {
      return srcString+srcString;
    } 

3.)客戶端獲取接口 

  private ServiceConnection connection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
      Log.e("TestService","TestService onServiceDisconnected()");
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      iTestInterface =  ITestInterface.Stub.asInterface(service);
      try {
        Log.e("TestService","TestService onServiceConnected()");
        int remoteId=iTestInterface.getProcessId();
        Log.e("TestService","TestService remoteId---->"+remoteId);
        int currentPid = android.os.Process.myPid();
        Log.e("TestService","TestService currentPid---->"+currentPid);
        Log.e("TestService","TestService dealString---->"+iTestInterface.dealString("Remote Service"));
        Log.e("TestService","TestService appendString---->"+iTestInterface.appendString("Remote Service"));
      } catch (RemoteException e) {
        e.printStackTrace();
      }
    }
  }; 

4.)通過IPC調(diào)用/傳遞數(shù)據(jù) 

        int remoteId=iTestInterface.getProcessId();
        Log.e("TestService","TestService remoteId---->"+remoteId);
        int currentPid = android.os.Process.myPid();
        Log.e("TestService","TestService currentPid---->"+currentPid);
        Log.e("TestService","TestService dealString---->"+iTestInterface.dealString("Remote Service"));
        Log.e("TestService","TestService appendString---->"+iTestInterface.appendString("Remote Service")); 

5.)Service聲明以及綁定/解綁  

聲明: 

    <service
      android:name=".TestService"
      android:enabled="true"
      android:exported="true"
      android:label="remoteService"
      android:process=":remote">
      <intent-filter android:priority="1000">
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="com.whoislcj.testaidl.TestService" />
      </intent-filter>
    </service> 

綁定:               

 Intent intent = new Intent("com.whoislcj.testaidl.TestService");
 intent.setPackage(getPackageName());//這里你需要設(shè)置你應(yīng)用的包名
 bindService(intent, connection, Context.BIND_AUTO_CREATE); 

解綁:unbindService(connection);

6.)訪問權(quán)限同Service一致

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

相關(guān)文章

最新評論