最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年03月27日 14:55:29   作者:_小馬快跑_  
本篇文章主要介紹了Android通過(guò)繼承Binder類(lèi)實(shí)現(xiàn)多進(jìn)程通信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

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

效果圖:

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

首先繼承Binder 類(lèi),實(shí)現(xiàn)onTransact()供客戶(hù)端調(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í),客戶(hù)端傳遞標(biāo)識(shí)執(zhí)行服務(wù)端代碼
     * @param data 客戶(hù)端傳遞過(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ù)器名稱(chēng)
          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>

客戶(hù)端代碼:BinderActivity.java:

首先編寫(xiě)ServiceConnection 類(lèi)來(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類(lèi)型數(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名稱(chēng):"
              + 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類(lèi)已經(jīng)實(shí)現(xiàn)了Pacelable接口序列化,進(jìn)程間傳輸數(shù)據(jù)就是一個(gè)數(shù)據(jù)序列化和反序列化的過(guò)程~

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

相關(guān)文章

最新評(píng)論

襄城县| 军事| 新密市| 喜德县| 桐柏县| 桑日县| 四川省| 五华县| 松溪县| 曲沃县| 平南县| 上杭县| 交口县| 洛扎县| 蕉岭县| 乐亭县| 犍为县| 驻马店市| 筠连县| 银川市| 防城港市| 枝江市| 西吉县| 凌海市| 涟水县| 当雄县| 无为县| 特克斯县| 乌什县| 涞源县| 永定县| 博爱县| 天门市| 靖州| 高碑店市| 荔波县| 垫江县| 微博| 体育| 三原县| 边坝县|