我编写了一个 PhoneGap Android 插件,并在那里打开了第二个活动:
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Context context = cordova.getActivity().getApplicationContext();
Intent intent = new Intent(context, secondActivity.class);
cordova.getActivity().startActivity(intent);
}
});
现在我想使用按钮关闭活动并将插件结果发送到 JavaScript,但我无法关闭活动并返回 PhoneGap 应用程序 - 我该如何执行此操作?
我希望有人能帮助我。感谢您的所有回答。
在你的插件中,使用startActivityForResult
来自 CordovaInterface 类而不是startActivity
来自安卓:
this.cordova.startActivityForResult(this,intent,0);
(0是一个int值,用于标识已启动的Activity,如果需要启动多个Activity,请使用其他数字)
在您的活动中,您添加以下函数以将结果返回到插件:
public void returnResult(int code, String result) {
Intent returnIntent = new Intent();
returnIntent.putExtra("result", result);
setResult(code, returnIntent);
finish();
}
因此,当您想要退出活动时,您可以使用 RESULT_CANCELED 或 RESULT_OK 以及表示您想要返回的字符串来调用此函数。
最后在您的插件类中添加以下函数:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case 0: //integer matching the integer suplied when starting the activity
if(resultCode == android.app.Activity.RESULT_OK){
//in case of success return the string to javascript
String result=intent.getStringExtra("result");
this.callbackContext.success(result);
}
else{
//code launched in case of error
String message=intent.getStringExtra("result");
this.callbackContext.error(message);
}
break;
default:
break;
}
}
希望这是您正在寻找的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)