Android通過繼承Binder類實現(xiàn)多進(jìn)程通信
AIDL的底層是通過Binder進(jìn)行通信的,通過追蹤.aidl編譯后自動生成的文件我們知道,文件中的Stub類用于服務(wù)端,Proxy類用于客戶端調(diào)用,那么可否直接通過繼承Binder類實現(xiàn)多進(jìn)程通信呢?下面就來試一試。
效果圖:
服務(wù)端代碼,BinderService.java:
首先繼承Binder 類,實現(xiàn)onTransact()供客戶端調(diào)用,同樣通過onBind()返回Binder實例:
private static final java.lang.String DESCRIPTOR = "org.ninetripods.mq.multiprocess_sever.IAidlCallBack"; private static final int KEY_FLAG = 0x110; private class MyBinder extends Binder { /** * @param code 唯一標(biāo)識,客戶端傳遞標(biāo)識執(zhí)行服務(wù)端代碼 * @param data 客戶端傳遞過來的參數(shù) * @param reply 服務(wù)器返回回去的值 * @param flags 是否有返回值 0:有 1:沒有 * @return * @throws RemoteException 異常 */ @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { switch (code) { case KEY_FLAG: //標(biāo)識服務(wù)器名稱 data.enforceInterface(DESCRIPTOR); Apple apple = new Apple("紅星蘋果", 15f, getString(R.string.response_binder_info)); reply.writeNoException(); reply.writeInt(1); apple.writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE); return true; } return super.onTransact(code, data, reply, flags); } } @Override public IBinder onBind(Intent intent) { return new MyBinder(); }
在AndroidManifest.xml中聲明一下:
<service android:name=".BinderService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.mq.binder.service" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>
客戶端代碼:BinderActivity.java:
首先編寫ServiceConnection 類來獲得Binder實例,來發(fā)送和接收數(shù)據(jù):
private ServiceConnection binderConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { isBound = true; mService = service; if (mService != null) { //聲明兩個Parcel類型數(shù)據(jù)(_data和_reply) 一個用于傳輸數(shù)據(jù) 一個用于接收數(shù)據(jù) android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); Apple apple; try { //與服務(wù)器端的enforceInterface(DESCRIPTOR)對應(yīng) _data.writeInterfaceToken(DESCRIPTOR); //調(diào)用服務(wù)端的transact()傳輸數(shù)據(jù) mService.transact(KEY_FLAG, _data, _reply, 0); _reply.readException(); if (0 != _reply.readInt()) { //接收服務(wù)端響應(yīng)數(shù)據(jù) apple = Apple.CREATOR.createFromParcel(_reply); } else { apple = null; } showMessage(apple != null ? ("\n" + apple.getNoticeInfo() + "\n名稱:" + apple.getName() + "\n價格:" + apple.getPrice() + " 元") : "未獲得服務(wù)器信息", R.color.red_f); } catch (Exception e) { e.printStackTrace(); } finally { _data.recycle(); _reply.recycle(); } } } @Override public void onServiceDisconnected(ComponentName name) { isBound = false; mService = null; } };
然后就是綁定服務(wù)了:
Intent intent = new Intent(); intent.setAction("android.mq.binder.service"); intent.setPackage("org.ninetripods.mq.multiprocess_sever"); bindService(intent, binderConnection, BIND_AUTO_CREATE);
代碼也挺簡單,里面用到的Apple類已經(jīng)實現(xiàn)了Pacelable接口序列化,進(jìn)程間傳輸數(shù)據(jù)就是一個數(shù)據(jù)序列化和反序列化的過程~
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
創(chuàng)建Android守護(hù)進(jìn)程實例(底層服務(wù))
這篇文章主要介紹了創(chuàng)建Android守護(hù)進(jìn)程實例(底層服務(wù)),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android6.0編程實現(xiàn)雙向通話自動錄音功能的方法詳解
這篇文章主要介紹了Android6.0編程實現(xiàn)雙向通話自動錄音功能的方法,結(jié)合實例形式分析了Android錄音功能的原理、實現(xiàn)技巧與相關(guān)注意事項,需要的朋友可以參考下2017-07-07android編程實現(xiàn)設(shè)置、打開wifi熱點共享供他人連接的方法
這篇文章主要介紹了android編程實現(xiàn)設(shè)置、打開wifi熱點共享供他人連接的方法,涉及Android創(chuàng)建WiFi及設(shè)置共享的相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11