作者:鹏城飞将 | 来源:互联网 | 2023-05-17 17:08
我有一个名为'Signature'的活动,我从CordovaPlugin调用它;
Plugin.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");
}
}
Signature.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回调.任何建议.谢谢
1> Blauharley..:
首先,有一些代码丢失(执行 -method的return语句),你必须告诉android/cordova-plugin等到结果发送回你的webview-app使用NO_RESULT和PluginResult的setKeepCallback否则cordova/android希望在执行 -method完成后立即获得结果:
Plugin.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());
}
这里有一个例子(对于你的插件类)和这里(对于你的签名类).
我的一个:这里和这里.