溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android多進程間采用AIDL方式進行通信

發(fā)布時間:2020-09-12 11:35:14 來源:腳本之家 閱讀:146 作者:愛好代碼 欄目:移動開發(fā)

在上一節(jié)中,我介紹了Android中Service的生命周期以及一些有關(guān)知識。在這一節(jié)中,我采用代碼編寫的方式來介紹一下不同程序之間也就是不同進程之間通信采用AIDL方式。

首先我需要解釋一下,不同程序進程間采用AIDL方式啟動服務(wù),我們可以看作成client客戶端與server服務(wù)端之間的通信,無非c/s都是安裝在了我們的智能手機設(shè)備Android系統(tǒng)之上。好了,理解到這里我們就可以繼續(xù)往下介紹了。

首先我們編寫server服務(wù)端程序:

1)我們新建一個應(yīng)用程序S,它的應(yīng)用程序架構(gòu)如下:

Android多進程間采用AIDL方式進行通信

2)我們在com.lgy.s包下編寫S.aidl文件,具體代碼如下:(aidl編碼格式不再敘述)

package com.lgy.s; 
 
interface S{ 
  String getStr(String name); 
} 

編寫好S.aidl文件我們就可以使用S.stub類下的相關(guān)方法。

3)我們可以自定義我們的Service了,具體代碼如下:

package com.lgy.s; 
 
import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.os.RemoteException; 
import android.util.Log; 
 
public class MyService extends Service { 
 
  private static final String TAG = "MyService"; 
  private S.Stub server; 
   
  @Override 
  public void onCreate() { 
    Log.i(TAG, "onCreate"); 
    server = new S.Stub() { 
      @Override 
      public String getStr(String name) throws RemoteException { 
        Log.i(TAG, name); 
        return name; 
      } 
    }; 
    super.onCreate(); 
  } 
   
  @Override 
  public boolean onUnbind(Intent intent) { 
    Log.i(TAG, "onUnbind"); 
    return super.onUnbind(intent); 
  } 
   
  @Override 
  public void onDestroy() { 
    Log.i(TAG, "onDestroy"); 
    server = null; 
    super.onDestroy(); 
  } 
   
   
  @Override 
  public IBinder onBind(Intent intent) { 
    Log.i(TAG, "onBind"); 
     
    return server; 
  } 
 
} 

4)我們進行服務(wù)端Server最后一步,在AndroidManifest.xml文件中注冊服務(wù)Service

<service android:name="com.lgy.s.MyService"> 
      <intent-filter > 
        <action android:name="android.lgy.myService" /> 
      </intent-filter> 
    </service> 

-----------到此我們服務(wù)器端就編寫完畢------------------------

下面我們編寫客戶端client應(yīng)用程序:

1)我們新建一個應(yīng)用程序C,具體應(yīng)用架構(gòu)如下:

Android多進程間采用AIDL方式進行通信

2)我們將在服務(wù)器端S寫的aidl原封不動的移到客戶端C上來(注包文件名都原封不動),移動后架構(gòu)如下圖:

Android多進程間采用AIDL方式進行通信

3)我們就可以在客戶端MainActivity中直接調(diào)用綁定服務(wù)器上的服務(wù),具體代碼如下:

package com.lgy.c; 
 
import android.app.Activity; 
import android.content.ComponentName; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.os.RemoteException; 
import android.util.Log; 
import android.view.View; 
 
import com.lgy.s.S; 
 
public class MainActivity extends Activity { 
   
  protected static final String TAG = "MainActivity"; 
  private S s; 
  private ServiceConnection conn = new ServiceConnection() { 
    @Override 
    public void onServiceDisconnected(ComponentName name) { 
       
    } 
     
    @Override 
    public void onServiceConnected(ComponentName name, IBinder service) { 
      s = S.Stub.asInterface(service); 
      Log.i(TAG, "onServiceConnected client"); 
    } 
  }; 
   
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
  } 
   
  public void bindBtn(View v){ 
    Intent mIntent = new Intent("android.lgy.myService"); 
    bindService(mIntent, conn, BIND_AUTO_CREATE); 
  } 
 
  public void greetBtn(View v){ 
    try { 
      Log.i(TAG, s.getStr("client")); 
    } catch (RemoteException e) { 
      e.printStackTrace(); 
    } 
  } 
   
  public void unbindBtn(View v){ 
    unbindService(conn); 
  } 
   
} 

4)MainActivity對應(yīng)的布局文件代碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  tools:context="com.lgy.c.MainActivity" > 
 
  <Button 
    android:id="@+id/button1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:onClick="bindBtn" 
    android:text="綁定服務(wù)器Service" /> 
 
  <Button 
    android:id="@+id/button2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/button1" 
    android:layout_below="@+id/button1" 
    android:onClick="greetBtn" 
    android:text="傳遞參數(shù)給服務(wù)器獲取返回的數(shù)據(jù)" /> 
 
  <Button 
    android:id="@+id/button3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/button2" 
    android:layout_below="@+id/button2" 
    android:onClick="unbindBtn" 
    android:text="解除綁定服務(wù)" /> 
 
</RelativeLayout> 

至此為止客戶端代碼我們已經(jīng)編寫完畢。

下面我們開始測試:

我們不運行服務(wù)器端,而是直接運行客戶端的話,相對應(yīng)的效果會怎么樣呢?具體效果如下解析:

第一、我們點擊綁定服務(wù)的話,系統(tǒng)程序無任何反應(yīng),這個時候在客戶端服務(wù)已經(jīng)綁定,但是沒有連接到服務(wù)端。接著我們再次點解讀取數(shù)據(jù)的話,系統(tǒng)將會崩潰。因為沒有連接到服務(wù)器端方法沒有具體實現(xiàn)。

第二、我們點擊綁定服務(wù)的話,系統(tǒng)程序無任何反應(yīng),這個時候在客戶端服務(wù)已經(jīng)綁定,但是沒有連接到服務(wù)端,接著我們點擊解除服務(wù)綁定的話,系統(tǒng)仍然沒有任何反應(yīng),我們要是再接著點擊解除服務(wù)綁定的話,系統(tǒng)就會崩潰,這也就從而再次證明了服務(wù)只會綁定一次,多次綁定的話服務(wù)不會做出任何反應(yīng);服務(wù)解除綁定只能僅只能解除綁定一次,多次解除綁定服務(wù)的話,系統(tǒng)就會崩潰。

第三、我們直接點擊接受數(shù)據(jù),系統(tǒng)程序也會崩潰,原因就是在于服務(wù)沒有綁定,服務(wù)端根本就沒有連接,相當于數(shù)據(jù)讀取方法沒有實現(xiàn)。

第四:我們直接點擊解除綁定的話,系統(tǒng)程序也會崩潰,原因就是在于服務(wù)一次也沒有綁定。

我們現(xiàn)在運行服務(wù)器,相對應(yīng)的效果又會怎么樣呢?具體效果如下解析:

第一、我們點擊綁定服務(wù),可以觀察到后臺logcat日志信息:

Android多進程間采用AIDL方式進行通信

Android多進程間采用AIDL方式進行通信

從日志我們可以看出在客戶端C綁定服務(wù)同時連接服務(wù)端,可以看到服務(wù)端Service的啟動onCreate和服務(wù)Service綁定onBind。

第二、我們點擊獲取數(shù)據(jù),可以觀察到后臺logcat日志信息:

Android多進程間采用AIDL方式進行通信

Android多進程間采用AIDL方式進行通信

從日志圖中我們可以看出客戶端將client字符串數(shù)據(jù)傳遞給服務(wù)器端,服務(wù)器端接受并返回一個字符串數(shù)據(jù)。

第三、我們點擊解除綁定服務(wù),具體logcat如下:

Android多進程間采用AIDL方式進行通信

第四:如果我們不點擊綁定服務(wù),而是直接點獲取數(shù)據(jù),或者解除綁定的話,系統(tǒng)都將會崩潰,具體原因前面已經(jīng)解釋清楚,在此不作過多重復(fù)。

以上就是AIDL在多進程中通信調(diào)用的簡單應(yīng)用(C應(yīng)用程序啟動S應(yīng)用程序服務(wù)Service)。

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

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI