Android asynchronous AIDL

左心房为你撑大大i 提交于 2019-12-12 03:59:17

问题


I have a main app and a remote service app that connects each other using AIDL.

I'm doing synchronous process successfully in remote service, but how can I do an asynchronous process?

My AIDL file is this:

import pack.addonlibrary.Action; /* parcelable */

interface IAddon {
    void onBind();
    void onUnbind();

    int getCallbacks();

    List<Action> onPageStarted(String url);
    List<Action> onPageFinished(String url);

    List<Action> onClicked(String url);

    List<Action> onUserConfirm(boolean cancelled);
    List<Action> onUserInput(String input, boolean cancelled);  

}

In my remote service, I want to this:

@Override
public List<Action> onClicked(String url) {   
  httpRequest() => onFinish() => showToastInClient(result)
  //shows toasts in main app
}

回答1:


You can implement AIDL asynchronously, by creating a new AIDL (for callbacks) as :

import pack.addonlibrary.Action;

oneway interface IAddOnCallback {

    void handleOnClicked(List<Action> actionList);
}

And by changing onClicked method of IAddon aidl class from:

List<Action> onClicked(String url);

to:

void onClicked(IAddOnCallback callback, String url);

After connecting to service and while calling onClicked method you can implement the callback object and can send that object to connecting service. Callback Stub (client) can be implemented as :

 IAddOnCallback.Stub callback = new IAddOnCallback.Stub() {

    @Override
    public void handleOnClicked(List<Action> actionList) throws RemoteException {
        // here you can perform actions
        //shows toasts in main app
    }
};

This is how you can do tasks asynchronously. Hope it helps.



来源:https://stackoverflow.com/questions/32363766/android-asynchronous-aidl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!