Android中的多進程通信怎么利用繼承Binder類實現(xiàn)?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)主營雙灤網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app開發(fā),雙灤h5微信小程序開發(fā)搭建,雙灤網(wǎng)站營銷推廣歡迎雙灤等地區(qū)企業(yè)咨詢
服務(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);看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
文章名稱:Android中的多進程通信怎么利用繼承Binder類實現(xiàn)
分享地址:http://www.jbt999.com/article10/gsedgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、動態(tài)網(wǎng)站、ChatGPT、企業(yè)建站、移動網(wǎng)站建設(shè)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)