gpt4 book ai didi

java - 从 Cordova 上的 Activity 回调

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:51:50 28 4
gpt4 key购买 nike

我有一个名为“签名”的 Activity ,我从 CordovaPlugin 调用它;

插件.java

public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException
{
Intent i = new Intent(context, Signature.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cordova.startActivityForResult(this,i,90);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.d(TAG, "activity result in plugin: requestCode(" + requestCode + "), resultCode(" + resultCode + ")");
if(requestCode == 90) {
if (resultCode == this.cordova.getActivity().RESULT_OK) {
Bundle res = intent.getExtras();
String result = res.getString("results");
Log.d("FIRST", "result:"+result);
this.callbackContext
.success(result.toString());
} else {
this.callbackContext.error("Error");
}
}

签名.java

private void finishWithResult(String result,int status)
{
Bundle conData = new Bundle();
conData.putString("results", result);
Intent intent = new Intent();
intent.putExtras(conData);
setResult(status, intent);
finish();
}

但是,当我调用“cordova.startActivityForResult”函数时,“onActivityResult”会立即自行调用它。我无法通过 finishWithResult 从 Activity 回调。任何建议。谢谢

最佳答案

首先,缺少一些代码(执行-方法的返回语句),您必须告诉 android/cordova-plugin 等到有结果发送回您的 webview-通过使用 PluginResultNO_RESULTsetKeepCallback 应用,否则 cordova/android 期望在执行时立即得到结果-方法已完成:

插件.java:

public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException
{

PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
callbackContext.sendPluginResult(r);

Intent i = new Intent(context, Signature.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cordova.startActivityForResult(this,i,90);

return true;

}

public void onActivityResult(int requestCode, int resultCode, Intent intent){
// here is your former code
...
...
// at last call sendPluginResult
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result.toString()));

// when there is no direct result form your execute-method use sendPluginResult because most plugins I saw and made recently (Reminder) prefer sendPluginResult to success/error
// this.callbackContext.success(result.toString());
}

举个例子here (对于您的插件类)和 here (对于你的签名类)。

还有我的一个:herehere .

关于java - 从 Cordova 上的 Activity 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29099912/

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