gpt4 book ai didi

Android Phonegap 3.4 : Notify javascript when an AsyncTask is finished

转载 作者:太空狗 更新时间:2023-10-29 14:15:56 24 4
gpt4 key购买 nike

我在 Android Phonegap: Notify javascript when an AsyncTask is finished 上遇到了与 Peacemoon 相同的问题

好吧,这适用于较旧的 PhoneGap 版本,但是......我的问题:这不适用于较新的 PhoneGap 版本,例如 PhoneGap 3.4。

插件类现在继承自CordovaPlugin,execute方法的返回类型现在是boolean,参数是CallbackContext而不是int callbackId等等。现在如何实现异步任务?

我的方法:

private CallbackContext myCallbackContext;

@Override
public boolean execute(String action, JSONArray args,
final CallbackContext callbackId)
{
this.myCallbackContext = callbackId;
if (action.equals("test"))
{
Intent intent = new Intent("com.companyname.name.TEST");
this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0);

// create an empty result, because the asynchronous call can take long
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
pluginResult.setKeepCallback(true);
this.myCallbackContext.sendPluginResult(pluginResult);

this.myCallbackContext.success;

return true;
}

public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0)
{
if (resultCode == Activity.RESULT_OK)
{
Bundle korb = intent.getExtras();
String osVersion = korb.getString("osVersion");


JSONObject obj = new JSONObject();
try
{
obj.put("osVersion", osVersion);
} catch(JSONException e)
{
// Log
}
PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
result.setKeepCallback(false);
this.myCallbackContext.sendPluginResult(result);

this.myCallbackContext.success(obj);
}

我对 JavaScript 的回归是不确定的。但它适用于我的旧版本和较旧的 PhoneGap 版本,如 1 .它如何在较新的 PhoneGap 版本上运行?

最佳答案

这是我的 phonegap 3.x 插件类的样子:

    public CallbackContext callbackContext;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;

if (action.equals("anAction")) {
Intent intent = new Intent(this.getActivity(),com.companyname.name.TEST.class);
if (this.cordova != null) {
this.cordova.startActivityForResult(this, intent, 0);
return true;
}
}
return false;
}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case 0:
//retour de la version utilisant le sample de imense alpr
if(resultCode == android.app.Activity.RESULT_OK){ //0:ok
String result=intent.getStringExtra("result");
this.callbackContext.success(result);
}
else{
String message=intent.getStringExtra("result");
this.callbackContext.error(message);
}

break;
default:
break;
}
}

关于Android Phonegap 3.4 : Notify javascript when an AsyncTask is finished,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22169574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com