這篇文章將為大家詳細講解有關怎么在Android中實現跨進程回掉接口,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
首先定義兩個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); }
服務端:
創建Service,并且在service中定義RemoteCallbackList集合,實現ITestInterface.Stub,在registerCallback,和unRegisterCallback中,分別將ITestCallBack對象注冊和反注冊進RemoteCallbackList中。RemoteCallbackList提供了獲取注冊進去的IInterface對象方法
//其實RemoteCallbackList類似于java中{@link java.util.Observable},用來批量處理接口回調對象, //其實如果確保只有一個客戶端會bind到這個服務,只需要保存一個IMyAidlInterfaceCallback即可。 //但是如果有多個,強烈推薦使用其實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(); } } //結束后一定要使用finsh,否則下次執行beginBroadcast會拋出IllegalStateException異常 mCallBacks.finishBroadcast(); }
在isTagValid中可以調用callBack方法去遍歷注冊的接口對象,也可以當服務端有變化時主動調用callBack方法去通知客戶端,這樣就實現了服務端變化主動通知客戶端??筛鶕嶋H方法修改。
在service的onBind方法中,返回ITestInterface.Stub的對象即可,等待客戶端綁定服務端。
下面是服務端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(); } }
客戶端:
客戶端首先要做的是綁定服務端,實現AIDL的通信,在客戶端創建綁定按鈕,解綁按鈕,和主動獲取信息的通信按鈕。在主動獲取信息的通信按鈕中實現iTestInterface對象的isTagValid方法可以主動去獲取服務端的信息(服務端在isTagValid方法中調用了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(); } } }
關于怎么在Android中實現跨進程回掉接口就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。