Android通過繼承Binder類實(shí)現(xiàn)多進(jìn)程通信
AIDL的底層是通過Binder進(jìn)行通信的,通過追蹤.aidl編譯后自動(dòng)生成的文件我們知道,文件中的Stub類用于服務(wù)端,Proxy類用于客戶端調(diào)用,那么可否直接通過繼承Binder類實(shí)現(xiàn)多進(jìn)程通信呢?下面就來試一試。
效果圖:

服務(wù)端代碼,BinderService.java:
首先繼承Binder 類,實(shí)現(xiàn)onTransact()供客戶端調(diào)用,同樣通過onBind()返回Binder實(shí)例:
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)識(shí),客戶端傳遞標(biāo)識(shí)執(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)識(shí)服務(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實(shí)例,來發(fā)送和接收數(shù)據(jù):
private ServiceConnection binderConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
isBound = true;
mService = service;
if (mService != null) {
//聲明兩個(gè)Parcel類型數(shù)據(jù)(_data和_reply) 一個(gè)用于傳輸數(shù)據(jù) 一個(gè)用于接收數(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價(jià)格:" + 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)實(shí)現(xiàn)了Pacelable接口序列化,進(jìn)程間傳輸數(shù)據(jù)就是一個(gè)數(shù)據(jù)序列化和反序列化的過程~
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
非常實(shí)用的小功能 Android應(yīng)用版本的更新實(shí)例
這篇文章主要為大家詳細(xì)介紹了一個(gè)非常實(shí)用的小功能,Android應(yīng)用版本的更新實(shí)例,感興趣的小伙伴們可以參考一下2016-08-08
Android?實(shí)現(xiàn)卡片堆疊錢包管理動(dòng)畫效果
這篇文章主要介紹了Android?實(shí)現(xiàn)卡片堆疊錢包管理動(dòng)畫效果,實(shí)現(xiàn)思路是在動(dòng)畫回調(diào)中requestLayout?實(shí)現(xiàn)動(dòng)畫效果,用Bounds?對象記錄每一個(gè)CardView?對象的初始位置,當(dāng)前位置,運(yùn)動(dòng)目標(biāo)位置,需要的朋友可以參考下2022-07-07
Android PickerView實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android PickerView實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)效果,PickerView實(shí)現(xiàn)全國地址的選擇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android開發(fā)基于ScrollView實(shí)現(xiàn)的漸變導(dǎo)航欄效果示例
這篇文章主要介紹了Android開發(fā)基于ScrollView實(shí)現(xiàn)的漸變導(dǎo)航欄效果,涉及ScrollView事件響應(yīng)及元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12
Android TouchListener實(shí)現(xiàn)拖拽刪實(shí)例代碼
這篇文章主要介紹了Android TouchListener實(shí)現(xiàn)拖拽刪實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android自定義ProgressBar實(shí)現(xiàn)漂亮的進(jìn)度提示框
這篇文章主要為大家詳細(xì)介紹了Android自定義ProgressBar實(shí)現(xiàn)漂亮的進(jìn)度提示框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Android自定義View實(shí)現(xiàn)帶音效和震動(dòng)的SeekBar
這篇文章主要為大家詳細(xì)介紹了Android如何自定義View實(shí)一個(gè)帶音效和震動(dòng)的SeekBar,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn)
這篇文章主要介紹了Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-01-01

