如何让Phonegap应用程序在Android上接收变量?

如何让Phonegap应用程序在Android上接收变量?,android,android-intent,cordova,Android,Android Intent,Cordova,在Stack OVerflow社区的大力帮助下,我现在已经成功地设置了我的Android应用程序,这样当你点击电子邮件中的链接时,它就会直接打开我的应用程序。换句话说,我在我的AndroidManifest.xml中正确配置了我的 到目前为止还不错,但还有最后一步我仍在努力,那就是让我的Phonegap内置应用程序拾取并使用我从电子邮件中的URL传递给它的变量 电子邮件中的链接如下所示: <a target="_blank" href="https://mysite.com/confirm

在Stack OVerflow社区的大力帮助下,我现在已经成功地设置了我的Android应用程序,这样当你点击电子邮件中的链接时,它就会直接打开我的应用程序。换句话说,我在我的
AndroidManifest.xml
中正确配置了我的

到目前为止还不错,但还有最后一步我仍在努力,那就是让我的Phonegap内置应用程序拾取并使用我从电子邮件中的URL传递给它的变量

电子邮件中的链接如下所示:

<a target="_blank" href="https://mysite.com/confirmation.html?verification=XXXXXXXXX&username=larry">Complete verification with MYAPP</a>

点击链接打开我的应用程序。准确地说,当用户点击它时,会弹出一个对话框,询问用户是想使用浏览器还是我的应用打开,这很酷

无论如何,关键是如果他们使用我的应用打开我的应用,它会打开默认的欢迎屏幕。不过,理想的行为是,该应用程序打开到一个特定页面,
confirmation.html
,并有两个字段填充URL中的变量,即验证和用户名

如何让我的Phonegap应用程序在正确的页面上启动并使用URL中的变量


重要提示:我真的想强调,这是一个Phonegap构建,这意味着该应用程序使用HTML和Javascript,而不是本机Android代码。感谢您的理解。

如果您使用的是
WebIntent
(我想您是这样做的?),您可以简单地执行以下操作:

window.plugins.webintent.getUri(function(url) {
        if(url !== "") {
            // url is the url the intent was launched with
            document.querySelector("#tag").innerHTML = "URL = " + url;
        }
    });
编辑:

好的,所以我设法让插件在PhoneGap2.9.0上运行

不过,我必须澄清一些事情

首先,您说过某些方法已被弃用,但是弃用并不意味着这些方法无法正常工作。github上列出的(旧)插件工作正常,但我也设法使它与CordovaPlugin一起工作

最新版本的插件的问题似乎在webintent.js文件中

我更改了几行代码来修复文件中发生的错误

/**
 * cordova Web Intent plugin
 * Copyright (c) Boris Smus 2010
 *
 */
 (function(cordova){
    var WebIntent = function() {        
    };

    WebIntent.prototype.ACTION_SEND = "android.intent.action.SEND";
    WebIntent.prototype.ACTION_VIEW= "android.intent.action.VIEW";
    WebIntent.prototype.EXTRA_TEXT = "android.intent.extra.TEXT";
    WebIntent.prototype.EXTRA_SUBJECT = "android.intent.extra.SUBJECT";
    WebIntent.prototype.EXTRA_STREAM = "android.intent.extra.STREAM";
    WebIntent.prototype.EXTRA_EMAIL = "android.intent.extra.EMAIL";

    WebIntent.prototype.startActivity = function(params, success, fail) {       
        return cordova.exec(function(args) {
            success(args);
        }, function(args) {
            fail(args);
        }, 'WebIntent', 'startActivity', [params]);
    };

    WebIntent.prototype.hasExtra = function(params, success, fail) {        
        return cordova.exec(function(args) {
            success(args);
        }, function(args) {
            fail(args);
        }, 'WebIntent', 'hasExtra', [params]);
    };

    WebIntent.prototype.getUri = function(success, fail) {      
        return cordova.exec(function(args) {
            success(args);
        }, function(args) {
            fail(args);
        }, 'WebIntent', 'getUri', []);
    };

    WebIntent.prototype.getExtra = function(params, success, fail) {        
        return cordova.exec(function(args) {
            success(args);
        }, function(args) {
            fail(args);
        }, 'WebIntent', 'getExtra', [params]);
    };


    WebIntent.prototype.onNewIntent = function(callback) {      
        return cordova.exec(function(args) {
            callback(args);
        }, function(args) {
        }, 'WebIntent', 'onNewIntent', []);
    };

    WebIntent.prototype.sendBroadcast = function(params, success, fail) {       
        return cordova.exec(function(args) {
            success(args);
        }, function(args) {
            fail(args);
        }, 'WebIntent', 'sendBroadcast', [params]);
    };

        window.webintent = new WebIntent();

        // backwards compatibility
        window.plugins = window.plugins || {};
        window.plugins.webintent = window.webintent;

})(window.PhoneGap || window.Cordova || window.cordova);
在WebIntent.java内部,我将一个变量更改为
private CallbackContext onNewIntentCallback=null

package com.borismus.webintent;

import java.util.HashMap;
import java.util.Map;

import org.apache.cordova.DroidGap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.text.Html;

import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.CallbackContext;

/**
 * WebIntent is a PhoneGap plugin that bridges Android intents and web
 * applications:
 *
 * 1. web apps can spawn intents that call native Android applications. 2.
 * (after setting up correct intent filters for PhoneGap applications), Android
 * intents can be handled by PhoneGap web applications.
 *
 * @author boris@borismus.com
 *
 */
public class WebIntent extends CordovaPlugin {

    private CallbackContext onNewIntentCallback = null;

    /**
     * Executes the request and returns PluginResult.
     *
     * @param action
     *            The action to execute.
     * @param args
     *            JSONArray of arguments for the plugin.
     * @param callbackContext
     *            The callbackContext used when calling back into JavaScript.
     * @return boolean
     */
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        try {
            if (action.equals("startActivity")) {
                if (args.length() != 1) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }

                // Parse the arguments
                JSONObject obj = args.getJSONObject(0);
                String type = obj.has("type") ? obj.getString("type") : null;
                Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;
                JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
                Map<String, String> extrasMap = new HashMap<String, String>();

                // Populate the extras if any exist
                if (extras != null) {
                    JSONArray extraNames = extras.names();
                    for (int i = 0; i < extraNames.length(); i++) {
                        String key = extraNames.getString(i);
                        String value = extras.getString(key);
                        extrasMap.put(key, value);
                    }
                }

                startActivity(obj.getString("action"), uri, type, extrasMap);
                callbackContext.success();
                return true;

            } else if (action.equals("hasExtra")) {
                if (args.length() != 1) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }
                Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
                String extraName = args.getString(0);
                PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
                callbackContext.sendPluginResult(res);
                return true;

            } else if (action.equals("getExtra")) {
                if (args.length() != 1) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }
                Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
                String extraName = args.getString(0);

                if (i.hasExtra(extraName)) {
                    PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
                    callbackContext.sendPluginResult(res);
                    return true;

                } else {
                    PluginResult res = new PluginResult(PluginResult.Status.ERROR);
                    callbackContext.sendPluginResult(res);
                    return false;
                }

            } else if (action.equals("getUri")) {
                if (args.length() != 0) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }

                Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
                String uri = i.getDataString();

                callbackContext.success(uri);
                return true;

            } else if (action.equals("onNewIntent")) {
                if (args.length() != 0) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }

                this.onNewIntentCallback = callbackContext;
                PluginResult res = new PluginResult(PluginResult.Status.NO_RESULT);
                res.setKeepCallback(true);
                callbackContext.sendPluginResult(res);
                return true;

            } else if (action.equals("sendBroadcast"))
            {
                if (args.length() != 1) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }

                // Parse the arguments
                JSONObject obj = args.getJSONObject(0);

                JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
                Map<String, String> extrasMap = new HashMap<String, String>();

                // Populate the extras if any exist
                if (extras != null) {
                    JSONArray extraNames = extras.names();
                    for (int i = 0; i < extraNames.length(); i++) {
                        String key = extraNames.getString(i);
                        String value = extras.getString(key);
                        extrasMap.put(key, value);
                    }
                }

                sendBroadcast(obj.getString("action"), extrasMap);
                callbackContext.success();
                return true;

            }

            PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
            callbackContext.sendPluginResult(res);
            return false;

        } catch (JSONException e) {
            callbackContext.error(e.getMessage());
            return false;
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        if (this.onNewIntentCallback != null) {
            this.onNewIntentCallback.success(intent.getDataString());
        }
    }

    void startActivity(String action, Uri uri, String type, Map<String, String> extras) {
        Intent i = (uri != null ? new Intent(action, uri) : new Intent(action));

        if (type != null && uri != null) {
            i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
        } else {
            if (type != null) {
                i.setType(type);
            }
        }

        for (String key : extras.keySet()) {
            String value = extras.get(key);
            // If type is text html, the extra text must sent as HTML
            if (key.equals(Intent.EXTRA_TEXT) && type.equals("text/html")) {
                i.putExtra(key, Html.fromHtml(value));
            } else if (key.equals(Intent.EXTRA_STREAM)) {
                // allowes sharing of images as attachments.
                // value in this case should be a URI of a file
                i.putExtra(key, Uri.parse(value));
            } else if (key.equals(Intent.EXTRA_EMAIL)) {
                // allows to add the email address of the receiver
                i.putExtra(Intent.EXTRA_EMAIL, new String[] { value });
            } else {
                i.putExtra(key, value);
            }
        }
        ((DroidGap)this.cordova.getActivity()).startActivity(i);
    }

    void sendBroadcast(String action, Map<String, String> extras) {
        Intent intent = new Intent();
        intent.setAction(action);
        for (String key : extras.keySet()) {
            String value = extras.get(key);
            intent.putExtra(key, value);
        }

        ((DroidGap)this.cordova.getActivity()).sendBroadcast(intent);
    }
}
package com.borismus.webintent;
导入java.util.HashMap;
导入java.util.Map;
导入org.apache.cordova.DroidGap;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.content.Intent;
导入android.net.Uri;
导入android.util.Log;
导入android.text.Html;
导入org.apache.cordova.api.CordovaPlugin;
导入org.apache.cordova.api.PluginResult;
导入org.apache.cordova.api.CallbackContext;
/**
*WebIntent是一个PhoneGap插件,用于连接Android意图和web应用程序
*应用程序:
*
* 1. web应用程序可以产生调用本机Android应用程序的意图。2.
*(在为PhoneGap应用程序设置了正确的意图过滤器之后),Android
*意图可以由PhoneGap web应用程序处理。
*
*@作者boris@borismus.com
*
*/
公共类WebIntent扩展了CordovaPlugin{
私有CallbackContext onNewIntentCallback=null;
/**
*执行请求并返回PluginResult。
*
*@param动作
*要执行的操作。
*@param args
*插件的一系列参数。
*@param callbackContext
*回调到JavaScript时使用的回调上下文。
*@返回布尔值
*/
公共布尔执行(字符串操作、JSONArray参数、CallbackContext CallbackContext){
试一试{
if(action.equals(“startActivity”)){
如果(参数长度()!=1){
PluginResult res=新的PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
返回false;
}
//解析参数
JSONObject obj=args.getJSONObject(0);
String type=obj.has(“type”)?obj.getString(“type”):null;
Uri=obj.has(“url”)?Uri.parse(obj.getString(“url”)):null;
JSONObject extras=obj.has(“extras”)?obj.getJSONObject(“extras”):null;
Map extrasMap=newhashmap();
//如果存在,则填充附加项
如果(附加值!=null){
JSONArray extraNames=extras.names();
对于(int i=0;i<!DOCTYPE html>
<html>
  <head>
    <title>Intent Test</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script src="webintent.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    function onLoad() {
        alert("onLoad");
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // device APIs are available
    //
    function onDeviceReady() {
        alert("onDeviceReady");
        window.plugins.webintent.getUri(function(url) {
            if(url !== "") {
                // url is the url the intent was launched with
                document.querySelector("#test").innerHTML = "URL was "+url;
            }
        });
    }

    </script>
  </head>
  <body onload="onLoad()">
<h1>Test</h1>

<div id="test"></div>
  </body>
</html>