在您的插件中,使用CordovaInterface类的startActivityForResult而不是Android的startActivity:
this.cordova.startActivityForResult(this,intent,0);
(0是一个int值,用于标识已启动的活动,如果需要启动多个活动,请使用其他数字)
在您的活动中,添加以下函数以将结果返回给插件:
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;
}
}
希望这就是您想要的.