国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android通過(guò)繼承Binder類實(shí)現(xiàn)多進(jìn)程通信

2019-12-12 03:16:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

效果圖:

服務(wù)端代碼,BinderService.java:

首先繼承Binder 類,實(shí)現(xiàn)onTransact()供客戶端調(diào)用,同樣通過(guò)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 客戶端傳遞過(guò)來(lái)的參數(shù)     * @param reply 服務(wù)器返回回去的值     * @param flags 是否有返回值 0:有 1:沒(méi)有     * @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("紅星蘋(píng)果", 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:

首先編寫(xiě)ServiceConnection 類來(lái)獲得Binder實(shí)例,來(lái)發(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)對(duì)應(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);

代碼也挺簡(jiǎn)單,里面用到的Apple類已經(jīng)實(shí)現(xiàn)了Pacelable接口序列化,進(jìn)程間傳輸數(shù)據(jù)就是一個(gè)數(shù)據(jù)序列化和反序列化的過(guò)程~

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 三明市| 南汇区| 沙湾县| 芦溪县| 洞头县| 麻栗坡县| 昂仁县| 安阳县| 襄樊市| 武平县| 杂多县| 临泉县| 兴安盟| 丰原市| 大田县| 紫金县| 会东县| 上思县| 萨嘎县| 南江县| 凤翔县| 鄂托克前旗| 泸西县| 治县。| 镇沅| 永昌县| 北辰区| 含山县| 德州市| 克山县| 盘山县| 沽源县| 新疆| 平顶山市| 大同县| 宣威市| 西峡县| 龙口市| 民丰县| 舟曲县| 雅江县|