Android 和 iOS 的 phonegap 原生插件生命周期如何?
假设我有 java/ObjectiveC 代码,它正在为我的 phonegap 应用程序做一些工作,但是在我的插件中,我启动了几个线程来支持一小部分主要工作。那么在 callbackContext.success(); 之后会发生什么?在 phonegap 收到结果后,我的线程仍在运行或我的 VM 将停止的环境是否仍然存在???
提前致谢。
最佳答案
只要 webview 活着,插件就活着,但默认情况下,插件只会触发一次回调
如果您的插件已初始化并且您希望它继续向 javascript 端发送成功或错误回调,您必须使用 setKeepCallback(true);
选项。为此,您需要在插件类中有一个 PluginResult
,然后使用 callbackContext.sendPluginResult()
而不是 callbackContext.success()
像这样:
private PluginResult pgResult= null;
private CallbackContext cbContext;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if ("test".equals(action)) {
//Do something here
cbContext = callbackContext;
pgResult = new PluginResult(PluginResult.Status.OK);
pgResult.setKeepCallback(true);
cbContext.sendPluginResult(pgResult);
return true;
}
return false; // Returning false results in a "MethodNotFound" error.
}
//This method can be called when you receive native events or something like that
public void anotherMethod() {
//Here you can continue calling the javascript callbacks
pgResult = new PluginResult(PluginResult.Status.OK);
pgResult.setKeepCallback(true);
cbContext.sendPluginResult(pgResult);
}
关于android - PhoneGap 原生插件生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710726/