如何获取Phonegap应用以在Android上接收变量?

如何获取Phonegap应用以在Android上接收变量?,第1张

概述在Stack OVerflow社区的帮助下,我现在已经成功设置了我的 Android应用程序,这样当点击电子邮件中的链接时,它就会直接打开我的应用程序.换句话说,我有我的< intent-filter>在我的AndroidManifest.xml中正确配置. 到目前为止一切都很好,但还有最后一步,我仍然在努力,这是让我的Phonegap构建的应用程序拿起并使用我从电子邮件中的URL传递给它的变量. 在Stack OVerflow社区的帮助下,我现在已经成功设置了我的 Android应用程序,这样当点击电子邮件中的链接时,它就会直接打开我的应用程序.换句话说,我有我的< intent-filter>在我的AndroIDManifest.xml中正确配置.

到目前为止一切都很好,但还有最后一步,我仍然在努力,这是让我的Phonegap构建的应用程序拿起并使用我从电子邮件中的URL传递给它的变量.

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

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

点击该链接可打开我的应用.确切地说,当单击它时,会d出一个对话框,询问用户是否要使用浏览器或我的应用程序打开,这很酷.

无论如何,重点是,如果他们使用我的应用程序打开我的应用程序,它将打开默认的欢迎屏幕.但是,所需的行为是应用程序打开特定页面,confirm.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;        }    });

编辑:

好的,所以我设法让插件在PhoneGap 2.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,fail) {                return cordova.exec(function(args) {            success(args);        },'hasExtra',[params]);    };    WebIntent.prototype.getUri = function(success,fail) {              return cordova.exec(function(args) {            success(args);        },'getUri',[]);    };    WebIntent.prototype.getExtra = function(params,'getExtra',[params]);    };    WebIntent.prototype.onNewIntent = function(callback) {              return cordova.exec(function(args) {            callback(args);        },function(args) {        },'onNewIntent',[]);    };    WebIntent.prototype.sendbroadcast = function(params,'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,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,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>

我用这段代码测试了它,我正确地收到了一个URL.如果你想我可以发给你整个项目(给我发电子邮件).希望这可以帮助.

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

总结

以上是内存溢出为你收集整理的如何获取Phonegap应用以在Android上接收变量?全部内容,希望文章能够帮你解决如何获取Phonegap应用以在Android上接收变量?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1140898.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存