我如何获得一个PhoneGap的应用程序,以获得在Android的变量? [英] How do I get a Phonegap app to receive variables on Android?

查看:219
本文介绍了我如何获得一个PhoneGap的应用程序,以获得在Android的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过从堆栈溢出倍受社会各界的帮助,我现在已经成功地建立了我的Andr​​oid应用程序,这样,当一个点击电子邮件中的链接,它会直接打开我的应用程序。换句话说,我有我的<意向滤光器> 在我的的Andr​​oidManifest.xml 正确配置。

With much help from the Stack OVerflow community, I now have successfully set up my Android app so that when one clicks on a link in an email, it opens my app directly. In other words, I have my <intent-filter> in my AndroidManifest.xml properly configured.

到目前为止好,但是,我仍然在努力,这让我的PhoneGap构建的应用程序,拿起和使用我传递给它的电子邮件中的URL中的变量最后一个步骤。

So far so good, but there is one last step that I am still struggling with, which is getting my Phonegap-built app to pick up and use the variables that I pass to it from a URL in an email.

在电子邮件中的链接看起来是这样的:

The link in the email looks like this:

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

点击链接打开了我的应用程序。为了precise,当人们点击它,会弹出一个对话框,询问用户是否希望使用浏览器或我的应用程序,这是酷开。

Clicking on the link opens up my app. To be precise, when one clicks on it, a dialog pops up asking the user whether they want to open using a browser or my app, which is cool.

总之,问题是,如果他们使用我的应用程序来打开我的应用程序,它会打开到默认的欢迎屏幕。所需的行为,虽然是应用程序打开到指定页面, confirmation.html ,并有两个领域充满了从URL,验证和用户名的变量。

Anyway, the point is that if they use my app to open my app, it opens to the default welcome screen. The desired behaviour, though, is that the app open to a specific page, confirmation.html, and have two fields filled with the variables from the URL, the verification and username.

我如何得到我的的PhoneGap 应用开始在右侧页面和URL中使用的变量?

How do I get my Phonegap app to start on the right page and use the variables in the URL?

重要提示:我真的想强调的,只是所以它是绝对清楚的,这是一个PhoneGap的构建,这意味着应用程序使用HTML和Javascript的没有的原生Android code。感谢您的谅解。

Important: I really want to stress, just so it's absolutely clear, this is a Phonegap build, meaning the app uses HTML and Javascript, not native Android code. Thanks for your understanding.

推荐答案

如果您使用的是 WebIntent (我假设你做的?),你可以简单的做到这一点:

If you are using WebIntent (which I assume you do?) you can simple do this:

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

编辑:

好了,所以我设法插件工作在PhoneGap的2.9.0。

Ok so I managed to get the plugin working on PhoneGap 2.9.0.

不过我要澄清一些事情。

However I have to clarify some things.

首先,你说,某些方法是pcated德$ P $,但德precation并不意味着该方法不会功能。 (旧)插件,因为它是列在github上工作得很好,但我还是设法得到它与CordovaPlugin工作为好。

First off, you said that certain methods were deprecated, however deprecation does not mean that the methods wont function. The (old) plugin as it is listed on github works fine, but I did manage to get it working with the CordovaPlugin as well.

与插件的最新版本,这个问题似乎是在webintent.js文件。

The problem with the lastest version of the plugin seems to be in the webintent.js file.

我改变了code几行来修复该文件中出现了错误。

I changed a few lines of code to fix an error that occurred inside the file.

/**
 * 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我改变一个变量私人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);
    }
}

和html文件。

<!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>

我这个code测试了它,我收到正确的URL。如果你想我可以给你的整个项目(给我发电子邮件)。希望这可以帮助。

I tested it with this code and I am correctly receiving an URL. If you want I can send you the whole project (email me). Hope this helps.

更新3.0: https://github.com/Initsogar/cordova-webintent

这篇关于我如何获得一个PhoneGap的应用程序,以获得在Android的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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