PhoneGap的Andr​​oid的插件 - 关闭插件活动 [英] PhoneGap Android Plugin - close the plugin Activity

查看:97
本文介绍了PhoneGap的Andr​​oid的插件 - 关闭插件活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了PhoneGap的Andr​​oid的插件,并在那里我打开第二个活动:

I have written a PhoneGap Android Plugin and there I open a second 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);
        }
    });

现在我想一个按钮关闭该活动并发送插件结果为JavaScript,但是我不能够关闭活动并返回到PhoneGap的应用程序 - 我该怎么办呢?

Now I'd like to close the activity with a button and send the Plugin Result to JavaScript, but Im not able to close the activity and go back to the PhoneGap Application - how can I do this?

我希望有人能帮助我。感谢所有的答案。

I hope somebody can help me. Thanks for all answers.

推荐答案

在你的插件,使用 startActivityForResult 从CordovaInterface类代替 startActivity 从Android的:

In your plugin, use startActivityForResult from CordovaInterface class instead of startActivity from Android:

this.cordova.startActivityForResult(this,intent,0);

(0是用于识别活动开始一个int值,如果需要启动多个活动中使用其他数字)

(0 is a int value used to identify the activity started, use other numbers if you need to start multiple activities)

在你的活动,你添加下面的函数返回一个结果给插件:

In your activity you add the following function to return a result to the plugin :

public void returnResult(int code, String result) {
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", result);
    setResult(code, returnIntent);
    finish();
}

所以,当你想退出你的活动你打电话与RESULT_CANCELED或RESULT_OK此功能,并重新presenting字符串你想回到什么。

So when you want to exit your activity you call this function with RESULT_CANCELED or RESULT_OK and a string representing what you want to return.

终于在你的插件类,添加以下功能:

And finally in your plugin class, add the following function :

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;
    }
}

希望这是你所期待的。

Hope it's what you were looking for.

这篇关于PhoneGap的Andr​​oid的插件 - 关闭插件活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆