欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)跨進(jìn)程接口回掉的方法

 更新時(shí)間:2019年05月08日 14:30:09   作者:H.Y.  
這篇文章主要給大家介紹了關(guān)于Android如何實(shí)現(xiàn)跨進(jìn)程接口回掉的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

        同一個(gè)進(jìn)程內(nèi)實(shí)現(xiàn)接口回掉很簡單,這里不做敘述,本文主要講的是跨進(jìn)程的接口回掉實(shí)現(xiàn)方式。有一種跨進(jìn)程通信的方式就是使用AIDL,但是單純的AIDL通信只可以實(shí)現(xiàn)客戶端訪問服務(wù)端主動獲取Binder對象,如果服務(wù)端有變化無法及時(shí)通知客戶端?,F(xiàn)在可以通過AIDL跨進(jìn)程接口回掉來解決服務(wù)端發(fā)生變化通知客戶端的問題。

         谷歌提供了RemoteCallbackList來實(shí)現(xiàn)對IInterface的管理。public class RemoteCallbackList<E extends IInterface>

首先定義兩個(gè)AIDL文件:

1.ITestCallBack.aidl

interface ITestCallBack {
 /**
  * Demonstrates some basic types that you can use as parameters
  * and return values in AIDL.
  */
 void onTagValid(in String tag);

}

2.ITestInterface.aidl    在注冊和反祖冊方法中,需要傳入ITestCallBack的對象

interface ITestInterface {
 /**
  * Demonstrates some basic types that you can use as parameters
  * and return values in AIDL.
  */
  boolean isTagValid(in String tag);

  void registerCallback(in String tag, in ITestCallBack callback);

  void unRegisterCallback(in String tag, in ITestCallBack callback);
}

服務(wù)端:

         創(chuàng)建Service,并且在service中定義RemoteCallbackList集合,實(shí)現(xiàn)ITestInterface.Stub,在registerCallback,和unRegisterCallback中,分別將ITestCallBack對象注冊和反注冊進(jìn)RemoteCallbackList中。RemoteCallbackList提供了獲取注冊進(jìn)去的IInterface對象方法

//其實(shí)RemoteCallbackList類似于java中{@link java.util.Observable},用來批量處理接口回調(diào)對象,
//其實(shí)如果確保只有一個(gè)客戶端會bind到這個(gè)服務(wù),只需要保存一個(gè)IMyAidlInterfaceCallback即可。
//但是如果有多個(gè),強(qiáng)烈推薦使用其實(shí)RemoteCallbackList

public void callBack() {
 if (mCallBacks == null) {
  return;
 }
 int num = mCallBacks.beginBroadcast();
 for (int i = 0; i < num; i++) {
  try {
   mCallBacks.getBroadcastItem(i).onTagValid("congratulation callback success " + tag);
  } catch (RemoteException e) {
   e.printStackTrace();
  }
 }
 //結(jié)束后一定要使用finsh,否則下次執(zhí)行beginBroadcast會拋出IllegalStateException異常
 mCallBacks.finishBroadcast();
}

在isTagValid中可以調(diào)用callBack方法去遍歷注冊的接口對象,也可以當(dāng)服務(wù)端有變化時(shí)主動調(diào)用callBack方法去通知客戶端,這樣就實(shí)現(xiàn)了服務(wù)端變化主動通知客戶端。可根據(jù)實(shí)際方法修改。

在service的onBind方法中,返回ITestInterface.Stub的對象即可,等待客戶端綁定服務(wù)端。

下面是服務(wù)端Service的代碼:

public class TestService extends Service {

 private RemoteCallbackList<ITestCallBack> mCallBacks = new RemoteCallbackList<>();
 private String tag = "hy";

 public TestService() {
 }

 @Override
 public IBinder onBind(Intent intent) {
  // TODO: Return the communication channel to the service.
  return iTestInterface;
 }

 @Override
 public boolean onUnbind(Intent intent) {
  return super.onUnbind(intent);
 }

 ITestInterface.Stub iTestInterface = new ITestInterface.Stub() {
  @Override
  public boolean isTagValid(String tag) throws RemoteException {
   if (tag.equals(TestService.this.tag)) {
    callBack();
    return true;
   }
   return false;
  }

  @Override
  public void registerCallback(String tag, ITestCallBack callback) throws RemoteException {
   if (null != mCallBacks && null != callback) {
    mCallBacks.register(callback);
   }
  }

  @Override
  public void unRegisterCallback(String tag, ITestCallBack callback) throws RemoteException {
   if (null != mCallBacks && null != callback) {
    mCallBacks.unregister(callback);
   }
  }
 };

 public void callBack() {
  if (mCallBacks == null) {
   return;
  }
  int num = mCallBacks.beginBroadcast();
  for (int i = 0; i < num; i++) {
   try {
    mCallBacks.getBroadcastItem(i).onTagValid("congratulation callback success " + tag);
   } catch (RemoteException e) {
    e.printStackTrace();
   }
  }
  mCallBacks.finishBroadcast();
 }
}

客戶端:

        客戶端首先要做的是綁定服務(wù)端,實(shí)現(xiàn)AIDL的通信,在客戶端創(chuàng)建綁定按鈕,解綁按鈕,和主動獲取信息的通信按鈕。在主動獲取信息的通信按鈕中實(shí)現(xiàn)iTestInterface對象的isTagValid方法可以主動去獲取服務(wù)端的信息(服務(wù)端在isTagValid方法中調(diào)用了callBack方法)。

客戶端代碼:

public class MainActivity extends AppCompatActivity {

 private String tag = "hy";
 private ITestInterface iTestInterface;
 private ServiceConnection connection = new ServiceConnection() {
  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   iTestInterface = ITestInterface.Stub.asInterface(service);
  }

  @Override
  public void onServiceDisconnected(ComponentName name) {
   iTestInterface = null;
  }
 };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  bindService();
  ((Button) findViewById(R.id.buttonregister)).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    try {
     iTestInterface.registerCallback(tag, new ITestCallBack.Stub() {
      @Override
      public void onTagValid(String tag) throws RemoteException {
       Log.e("test", "registerCallback: " + tag);
      }
     });
    } catch (RemoteException e) {
     e.printStackTrace();
    }
   }
  });

  ((Button) findViewById(R.id.buttonunregister)).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    try {
     iTestInterface.unRegisterCallback(tag, new ITestCallBack.Stub() {
      @Override
      public void onTagValid(String tag) throws RemoteException {

      }
     });
    } catch (RemoteException e) {
     e.printStackTrace();
    }
   }
  });

  ((Button) findViewById(R.id.buttonisvalid)).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    try {
     iTestInterface.isTagValid(tag);
    } catch (RemoteException e) {
     e.printStackTrace();
    }
   }
  });
 }

 private void bindService() {
  Intent intent = new Intent();
  intent.setAction("com.example.heyang.myapplication.TestService");
  intent.setPackage("com.example.heyang.myapplication");
  boolean success = bindService(intent, connection, Context.BIND_AUTO_CREATE);
  if (success) {
   Log.e("test ", "bindService OK");
  } else {
   Log.e("test ", "bindService Fail");
  }
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  unBindeService();
 }

 private void unBindeService() {
  try {
   unbindService(connection);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論