Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 使用 AIDL 實現跨進程調用 Service 的完整教學

編輯:關於 Android 程式設計

一、問題描述

Android 應用程式的四大組件——Activity、BroadcastReceiver、ContentProvider、Service,都可以進行跨進程。上一篇我們透過 ContentProvider 實現了不同應用之間的跨進程調用,但 ContentProvider 主要用於資料共享(如 SQLite 資料庫)。那麼,如果我們想跨進程調用服務(Service)呢?Android 系統採用遠端程序呼叫(RPC)方式來實現。與許多基於 RPC 的方案一樣,Android 使用一種介面定義語言(Interface Definition Language,IDL)來公開服務的介面。Service 的跨進程調用需要透過 AIDL 來實現,AIDL 服務應用非常廣泛,例如百度地圖 API 就提供了跨進程的服務。以下就來看看如何實現 AIDL Service,案例如下圖:

二、實現 AIDL 服務的步驟

1. 編寫 AIDL 檔;
2. 若 aidl 檔內容正確,會自動產生一個 Java 介面檔(*.java);
3. 建立一個服務類別(Service 的子類);
4. 實作由 aidl 檔產生的 Java 介面;
5. 在 AndroidManifest.xml 中配置 AIDL 服務,為 <action> 標籤加上 android:name,以便客戶端使用隱式 Intent 啟動服務;
6. 撰寫客戶端。

三、編寫 AIDL 檔

Android 介面定義語言——LocalService.aidl:

 package com.jereh.remote; interface LocalService{ String getLocal(); }

IDE 會自動產生 LocalService.java 檔,如下圖所示:

四、Remote 應用實作

1、編寫 MyRemoteService

這裡關鍵在於 onBind 回傳一個繼承自 LocalService.Stub 的 Binder 實作,並在其中覆寫 getLocal 方法。

 public class MyRemoteService extends Service { @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return new MyRemoteServiceImpl(); } private class MyRemoteServiceImpl extends LocalService.Stub{ @Override public String getLocal() throws RemoteException { // TODO Auto-generated method stub return "煙台傑瑞教育"; } } } 

2、AndroidManifest.xml 配置

這裡透過 android:process="remote" 讓 Service 運行在獨立進程,並以 intent-filter 的 action 供客戶端隱式 Intent 綁定。

 <service android:name="com.jereh.retmote.MyRemoteService" android:process="remote" > <intent-filter> <action android:name="com.jereh.remote_service"/> </intent-filter> </service>

五、客戶端實作

1、在客戶端應用中加入 LocalService.aidl

注意包名要與服務端定義的相同,如下圖:

同樣會自動產生 LocalService.java 程式碼。

2、MainActivity 程式碼:

客戶端透過 bindService 綁定遠端服務,並在 onServiceConnected 回呼中以 LocalService.Stub.asInterface(binder) 取得遠端介面代理,之後即可像呼叫本地方法一樣調用 getLocal。

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void startService(View view){ Intent service=new Intent("com.jereh.remote_service"); super.bindService(service, conn, Context.BIND_AUTO_CREATE); } public void showInfo(View view){ try { local=service.getLocal(); Log.d("jereh", local); Toast.makeText(this, "您已進入"+local,Toast.LENGTH_LONG).show(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private LocalService service; private String local; private ServiceConnection conn=new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName arg0) { } @Override public void onServiceConnected(ComponentName name, IBinder binder) { // TODO Auto-generated method stub // 獲取遠程Service的onBinder方法返回的對象代理 service=LocalService.Stub.asInterface(binder); } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

xml 檔:

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="啟動遠程服務" android:onClick="startService" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看信息" android:onClick="showInfo" /> </LinearLayout>

以上所述就是本文介紹的「Android 應用程式四大組件之使用 AIDL 實現跨進程調用 Service」。透過 AIDL 檔定義介面、Stub 實作 Binder、再以 bindService 綁定,即可在不同進程之間安全地溝通,希望大家喜歡。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved