我已经编写了一个 PhoneGap Android 插件并在那里打开了第二个 Activity :
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);
}
});
现在我想用一个按钮关闭 Activity 并将插件结果发送到 JavaScript,但我无法关闭 Activity 并返回到 PhoneGap 应用程序 - 我该怎么做?
我希望有人能帮助我。感谢您的所有回答。
最佳答案
在你的插件中,使用startActivityForResult
来自 CordovaInterface 类而不是 startActivity
来自安卓:
this.cordova.startActivityForResult(this,intent,0);
(0是一个int值,用于标识启动的activity,如果需要启动多个activity,则使用其他数字)
在您的 Activity 中,您添加以下函数以将结果返回给插件:
public void returnResult(int code, String result) {
Intent returnIntent = new Intent();
returnIntent.putExtra("result", result);
setResult(code, returnIntent);
finish();
}
因此,当您想要退出您的 Activity 时,您可以使用 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;
}
}
希望这就是您要找的。p>
关于android - PhoneGap Android Plugin - 关闭插件 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21858861/