編輯:關於 Android 開發
Android 系統四大元件——Activity、Content Provider、Broadcast 與 Service,都能進行跨進程資料傳輸。Activity 可隱式呼叫其他進程的 Activity;Content Provider 可跨進程存取其他應用中的資料;Broadcast 透過廣播與其他應用通訊;Service 則透過 Binder 實現 RPC。
Binder 的實現機制相當複雜,這裡不展開,只需知道它以 C/S 模式實現 RPC(Remote Procedure Call Protocol,遠程程序呼叫協議)。舉例來說,應用程式 A 實作了加減乘除的操作並對外提供介面,其他應用程式就能遠程呼叫 A 提供的介面完成運算,而無須自行實作。
那麼 AIDL 又是什麼?AIDL 是 Android Interface Definition Language 的縮寫,即 Android 介面定義語言。如果把 Binder 比作一條管道,AIDL 便定義了這條管道中傳輸資料的格式。
1、在 Eclipse 中新建 Android 工程,命名為 Service。在 src 目錄下右鍵新建套件 com.example.aidl,並在其中新增檔案 IRemoteService.aidl,標明該服務對外提供 getPid() 與 basicTypes() 兩個介面:
package com.example.aidl; interface IRemoteService { /** Request the process ID of this service, to do evil things with it. */ int getPid(); /** Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ int basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); }
aidl 檔完成後,gen 目錄下會自動生成 com.example.aidl 套件與 IRemoteService.java,無須手動修改。接著實作服務類別:
package com.example.service; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.Process; import android.util.Log; import com.example.aidl.IRemoteService; public class AIDLService extends Service { private String TAG = "REMOTESERVICE"; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Log.d(TAG, "DDService onBind"); return mBinder; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.d(TAG, "DDService onCreate........" + "Thread: " + Thread.currentThread().getName()); } /*與本地服務不同,此處新建Stub類型的Binder*/ private final IRemoteService.Stub mBinder = new IRemoteService.Stub() { public int getPid(){ Log.d(TAG, "Thread: " + Thread.currentThread().getName()); /*返回給調用者當前的線程編號*/ return (int) Thread.currentThread().getId(); } public int basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) { Log.d(TAG, "Thread: " + Thread.currentThread().getName()); Log.d(TAG, "basicTypes aDouble: " + aDouble +" anInt: " + anInt+" aBoolean " + aBoolean+" aString " + aString); /*返回給調用者當前的線程編號*/ return (int) Thread.currentThread().getId(); } }; }
可以看到,與本地服務不同的是,我們在 onBind() 中回傳的是 IRemoteService.Stub 類型的變數。Stub 實際上繼承自 Binder,並定義了我們在 AIDL 中宣告的介面,需要我們去實作這些介面,即 getPid() 與 basicTypes()。
3、在 AndroidManifest.xml 中宣告該服務:
<service android:name=".AIDLService" android:process=":xlzh" > <intent-filter> <action android:name="com.example.aidl" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>
此處在 Service 屬性中增加了 android:process,冒號後可自訂名稱。至此,服務端的工作全部完成。
1、在 Eclipse 中新建工程,命名為 Client,然後把 Server 工程中的 com.example.aidl 套件整個複製到 Client 的 src 目錄下。
2、在版面配置中放置「bind」與「unbind」兩個按鈕。
3、在 Activity 中使用遠程服務,MainActivity.java 內容如下:
package com.example.client; import android.app.Activity; import android.content.ComponentName; import android.content.Context; 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 android.widget.Button; import com.example.aidl.IRemoteService; public class MainActivity extends Activity { private String TAG = "com.example.aidl"; private IRemoteService remoteService; private Button mBindButton; private Button mUnbindButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBindButton = (Button)findViewById(R.id.binderButton); mBindButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d(TAG, "Bind Service"); //Intent intent = new Intent(IRemoteService.class.getName()); /*新建Intent,指定Service的AndroidMainfest.xml中聲明的Action名稱*/ Intent intent = new Intent("com.example.aidl"); /*在Android5.0以後,不允許隱式調用服務,故再次指定服務所在包名*/ intent.setPackage("com.example.service"); /*綁定服務*/ bindService(intent, conn, Context.BIND_AUTO_CREATE); } }); mUnbindButton = (Button)findViewById(R.id.unbinderButton); mUnbindButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d(TAG, "UnBind Service"); /*解綁服務*/ unbindService(conn); } }); } ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Log.d(TAG, "UnBind Service success!"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d(TAG, "Bind Service success!"); /*獲取服務端handle*/ remoteService = IRemoteService.Stub.asInterface(service); try { int pid1 = remoteService.getPid(); int pid2 = remoteService.basicTypes(12, 1223, true, 12.2f, 12.3, "我們的愛,我明白"); /*打印帶有getPid接口和basicTypes接口時服務端的線程號*/ Log.d(TAG, "remoteService.getPid(): " + pid1 + " remoteService.basicTypes(): " + pid2); } catch (RemoteException e) { e.printStackTrace(); } } }; @Override protected void onDestroy() { super.onDestroy(); /*正規做法是設置標記位,標志服務是否已經unbind,來決定是否調用unbindService * 此處,簡便行事,默認此時服務已經unbind*/ //unbindService(conn); } }
1、Android 5.0 之後不允許隱式呼叫服務,因此必須明確指定要呼叫的服務所在的套件名稱(這一點讓我調了一下午才調通)。
2、從客戶端的列印結果可知,當呼叫 getPid 與 basicTypes 介面時,伺服器端處理呼叫的線程並不相同,具體原因需深入 Binder 機制才能理解。
3、與本地服務的差異在於:伺服器端回傳的是繼承自 Binder 的 Stub 型別 Binder,客戶端透過 Stub 的 asInterface 介面取得伺服器 handle;此外,伺服器端在 AndroidManifest.xml 中宣告服務時需指定 android:process 屬性。
4、AIDL 支援的資料類型包括:Java 原生類型;String 與 CharSequence;List 與 Map(元素須為 AIDL 支援的類型);AIDL 自動生成的介面;實作 android.os.Parcelable 介面的類別。其中前三者無須 import,後兩者則需要。
使用遠程服務的流程如下:
1、定義 AIDL 檔,宣告該服務對外提供的介面;
2、在伺服器中實作 AIDL 中定義的介面;
3、在 AndroidManifest.xml 中宣告遠程服務;
4、在客戶端複製伺服器的 AIDL 檔;
5、在客戶端指定伺服器的服務名稱與所在套件,進行綁定;
6、客戶端透過 Stub.asInterface 取得伺服器 handle,呼叫服務提供的介面。
仿淘寶商品屬性標籤頁:自定義 ViewGroup 自動換行
仿淘寶商品屬性標籤頁:自定義 ViewGroup 自動換行電商商品頁常需要動態顯示顏色、尺碼、款式等屬性,且每個屬性下的標籤數量不固定。若使用普通 LinearLayou
Docker 跨主機方案 Weave 的踩坑記錄:fastdp 與 virtio_net 相容性問題
問題背景 Weave 在 1.2 之後加入 fastdp 模式,預設使用核心的 Open vSwitch 與 VXLAN 進行封裝。若在特定核心版本的 CentOS 7
深入 Android 啟動流程:從 Linux 核心到桌面介面
Android 手機系統在本質上是一個以 Linux 核心為基礎的應用程式環境。正因如此,按下電源鍵到看見桌面圖示的這段路,其實可以拆成兩段漫長的旅程:先由 Linux
【微學堂】第22期:Nginx 的性能優化
【微學堂】第22期:Nginx 的性能優化分享主題:Nginx 的性能優化分享時間:2016 年 11 月 24 日(周四)20:00特邀講師:李強(撒加)講師簡介:運維