Android异步AIDL [英] Android asynchronous AIDL
问题描述
我有一个主应用程序和一个远程服务应用程序,它们使用AIDL相互连接.
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?
我的AIDL文件是这样的:
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
}
推荐答案
通过创建新的AIDL(用于回调),您可以异步实现AIDL:
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);
}
并通过以下方式更改 IAddon
aidl类的 onClicked
方法:
And by changing onClicked
method of IAddon
aidl class from:
List<Action> onClicked(String url);
收件人:
void onClicked(IAddOnCallback callback, String url);
连接到服务后并调用 onClicked
方法时,您可以实现回调对象并将该对象发送到连接服务.回调存根(客户端)可以实现为:
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.
这篇关于Android异步AIDL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!