问题
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