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

首頁 > 系統 > Android > 正文

Android通過繼承Binder類實現多進程通信

2019-10-23 18:33:29
字體:
來源:轉載
供稿:網友

AIDL的底層是通過Binder進行通信的,通過追蹤.aidl編譯后自動生成的文件我們知道,文件中的Stub類用于服務端,Proxy類用于客戶端調用,那么可否直接通過繼承Binder類實現多進程通信呢?下面就來試一試。

效果圖:

android,多進程通信,多進程,binder

服務端代碼,BinderService.java:

首先繼承Binder 類,實現onTransact()供客戶端調用,同樣通過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 唯一標識,客戶端傳遞標識執行服務端代碼     * @param data 客戶端傳遞過來的參數     * @param reply 服務器返回回去的值     * @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:          //標識服務器名稱          data.enforceInterface(DEscriptOR);          Apple apple = new Apple("紅星蘋果", 15f, getString(R.string.response_binder_info));          reply.writeNoException();          reply.writeInt(1);          apple.writeToParcel(reply, android/216528.html">android/176373.html">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實例,來發送和接收數據:

private ServiceConnection binderConnection = new ServiceConnection() {    @Override    public void onServiceConnected(ComponentName name, IBinder service) {      isBound = true;      mService = service;      if (mService != null) {        //聲明兩個Parcel類型數據(_data和_reply) 一個用于傳輸數據 一個用于接收數據        android.os.Parcel _data = android.os.Parcel.obtain();        android.os.Parcel _reply = android.os.Parcel.obtain();        Apple apple;        try {          //與服務器端的enforceInterface(DEscriptOR)對應          _data.writeInterfaceToken(DEscriptOR);          //調用服務端的transact()傳輸數據          mService.transact(KEY_FLAG, _data, _reply, 0);          _reply.readException();          if (0 != _reply.readInt()) {            //接收服務端響應數據            apple = Apple.CREATOR.createFromParcel(_reply);          } else {            apple = null;          }          showMessage(apple != null ? ("/n" + apple.getNoticeInfo() + "/n名稱:"              + apple.getName() + "/n價格:" + apple.getPrice() + " 元") : "未獲得服務器信息", R.color.red_f);        } catch (Exception e) {          e.printStackTrace();        } finally {          _data.recycle();          _reply.recycle();        }      }    }    @Override    public void onServiceDisconnected(ComponentName name) {      isBound = false;      mService = null;    }  };

然后就是綁定服務了:

 Intent intent = new Intent(); intent.setAction("android.mq.binder.service"); intent.setPackage("org.ninetripods.mq.multiprocess_sever"); bindService(intent, binderConnection, BIND_AUTO_CREATE);

代碼也挺簡單,里面用到的Apple類已經實現了Pacelable接口序列化,進程間傳輸數據就是一個數據序列化和反序列化的過程~

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 章丘市| 平定县| 林州市| 卢氏县| 武定县| 盖州市| 易门县| 洪雅县| 兰溪市| 蒲江县| 江陵县| 文成县| 刚察县| 乌什县| 辽宁省| 齐齐哈尔市| 清苑县| 信阳市| 柯坪县| 天全县| 时尚| 上饶市| 惠安县| 武乡县| 漾濞| 临高县| 板桥市| 廉江市| 阳谷县| 太原市| 金溪县| 东乌珠穆沁旗| 荆州市| 闽侯县| 水富县| 清原| 佛坪县| 桃源县| 朝阳县| 饶阳县| 进贤县|