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

前端之家收集整理的这篇文章主要介绍了如何获取Phonegap应用以在Android上接收变量?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Stack OVerflow社区的帮助下,我现在已经成功设置了我的 Android应用程序,这样当点击电子邮件中的链接时,它就会直接打开我的应用程序.换句话说,我有我的< intent-filter>在我的AndroidManifest.xml中正确配置.

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

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

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

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

无论如何,重点是,如果他们使用我的应用程序打开我的应用程序,它将打开默认的欢迎屏幕.但是,所需的行为是应用程序打开特定页面,confirm.html,并且有两个字段填充了URL,验证和用户名中的变量.

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

重要提示:我真的很想强调,这是绝对清楚的,这是一个Phonegap构建,这意味着该应用程序使用HTML和Javascript,而不是原生的Android代码.谢谢你的理解.

解决方法

如果您正在使用WebIntent(我假设您这样做了吗?),您可以这样做:
  1. window.plugins.webintent.getUri(function(url) {
  2. if(url !== "") {
  3. // url is the url the intent was launched with
  4. document.querySelector("#tag").innerHTML = "URL = " + url;
  5. }
  6. });

编辑:

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

但是我必须澄清一些事情.

首先,您说某些方法已被弃用,但弃用并不意味着这些方法不起作用.在github上列出的(旧)插件工作正常,但我确实设法让它与CordovaPlugin一起工作.

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

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

  1. /**
  2. * cordova Web Intent plugin
  3. * Copyright (c) Boris Smus 2010
  4. *
  5. */
  6. (function(cordova){
  7. var WebIntent = function() {
  8. };
  9.  
  10. WebIntent.prototype.ACTION_SEND = "android.intent.action.SEND";
  11. WebIntent.prototype.ACTION_VIEW= "android.intent.action.VIEW";
  12. WebIntent.prototype.EXTRA_TEXT = "android.intent.extra.TEXT";
  13. WebIntent.prototype.EXTRA_SUBJECT = "android.intent.extra.SUBJECT";
  14. WebIntent.prototype.EXTRA_STREAM = "android.intent.extra.STREAM";
  15. WebIntent.prototype.EXTRA_EMAIL = "android.intent.extra.EMAIL";
  16.  
  17. WebIntent.prototype.startActivity = function(params,success,fail) {
  18. return cordova.exec(function(args) {
  19. success(args);
  20. },function(args) {
  21. fail(args);
  22. },'WebIntent','startActivity',[params]);
  23. };
  24.  
  25. WebIntent.prototype.hasExtra = function(params,fail) {
  26. return cordova.exec(function(args) {
  27. success(args);
  28. },'hasExtra',[params]);
  29. };
  30.  
  31. WebIntent.prototype.getUri = function(success,fail) {
  32. return cordova.exec(function(args) {
  33. success(args);
  34. },'getUri',[]);
  35. };
  36.  
  37. WebIntent.prototype.getExtra = function(params,'getExtra',[params]);
  38. };
  39.  
  40.  
  41. WebIntent.prototype.onNewIntent = function(callback) {
  42. return cordova.exec(function(args) {
  43. callback(args);
  44. },function(args) {
  45. },'onNewIntent',[]);
  46. };
  47.  
  48. WebIntent.prototype.sendBroadcast = function(params,'sendBroadcast',[params]);
  49. };
  50.  
  51. window.webintent = new WebIntent();
  52.  
  53. // backwards compatibility
  54. window.plugins = window.plugins || {};
  55. window.plugins.webintent = window.webintent;
  56.  
  57. })(window.PhoneGap || window.Cordova || window.cordova);

在WebIntent.java中,我将一个变量更改为私有CallbackContext onNewIntentCallback = null;

  1. package com.borismus.webintent;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.apache.cordova.DroidGap;
  7. import org.json.JSONArray;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10.  
  11. import android.content.Intent;
  12. import android.net.Uri;
  13. import android.util.Log;
  14. import android.text.Html;
  15.  
  16. import org.apache.cordova.api.CordovaPlugin;
  17. import org.apache.cordova.api.PluginResult;
  18. import org.apache.cordova.api.CallbackContext;
  19.  
  20. /**
  21. * WebIntent is a PhoneGap plugin that bridges Android intents and web
  22. * applications:
  23. *
  24. * 1. web apps can spawn intents that call native Android applications. 2.
  25. * (after setting up correct intent filters for PhoneGap applications),Android
  26. * intents can be handled by PhoneGap web applications.
  27. *
  28. * @author boris@borismus.com
  29. *
  30. */
  31. public class WebIntent extends CordovaPlugin {
  32.  
  33. private CallbackContext onNewIntentCallback = null;
  34.  
  35. /**
  36. * Executes the request and returns PluginResult.
  37. *
  38. * @param action
  39. * The action to execute.
  40. * @param args
  41. * JSONArray of arguments for the plugin.
  42. * @param callbackContext
  43. * The callbackContext used when calling back into JavaScript.
  44. * @return boolean
  45. */
  46. public boolean execute(String action,JSONArray args,CallbackContext callbackContext) {
  47. try {
  48. if (action.equals("startActivity")) {
  49. if (args.length() != 1) {
  50. PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
  51. callbackContext.sendPluginResult(res);
  52. return false;
  53. }
  54.  
  55. // Parse the arguments
  56. JSONObject obj = args.getJSONObject(0);
  57. String type = obj.has("type") ? obj.getString("type") : null;
  58. Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;
  59. JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
  60. Map<String,String> extrasMap = new HashMap<String,String>();
  61.  
  62. // Populate the extras if any exist
  63. if (extras != null) {
  64. JSONArray extraNames = extras.names();
  65. for (int i = 0; i < extraNames.length(); i++) {
  66. String key = extraNames.getString(i);
  67. String value = extras.getString(key);
  68. extrasMap.put(key,value);
  69. }
  70. }
  71.  
  72. startActivity(obj.getString("action"),uri,type,extrasMap);
  73. callbackContext.success();
  74. return true;
  75.  
  76. } else if (action.equals("hasExtra")) {
  77. if (args.length() != 1) {
  78. PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
  79. callbackContext.sendPluginResult(res);
  80. return false;
  81. }
  82. Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
  83. String extraName = args.getString(0);
  84. PluginResult res = new PluginResult(PluginResult.Status.OK,i.hasExtra(extraName));
  85. callbackContext.sendPluginResult(res);
  86. return true;
  87.  
  88. } else if (action.equals("getExtra")) {
  89. if (args.length() != 1) {
  90. PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
  91. callbackContext.sendPluginResult(res);
  92. return false;
  93. }
  94. Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
  95. String extraName = args.getString(0);
  96.  
  97. if (i.hasExtra(extraName)) {
  98. PluginResult res = new PluginResult(PluginResult.Status.OK,i.hasExtra(extraName));
  99. callbackContext.sendPluginResult(res);
  100. return true;
  101.  
  102. } else {
  103. PluginResult res = new PluginResult(PluginResult.Status.ERROR);
  104. callbackContext.sendPluginResult(res);
  105. return false;
  106. }
  107.  
  108. } else if (action.equals("getUri")) {
  109. if (args.length() != 0) {
  110. PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
  111. callbackContext.sendPluginResult(res);
  112. return false;
  113. }
  114.  
  115. Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
  116. String uri = i.getDataString();
  117.  
  118. callbackContext.success(uri);
  119. return true;
  120.  
  121. } else if (action.equals("onNewIntent")) {
  122. if (args.length() != 0) {
  123. PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
  124. callbackContext.sendPluginResult(res);
  125. return false;
  126. }
  127.  
  128. this.onNewIntentCallback = callbackContext;
  129. PluginResult res = new PluginResult(PluginResult.Status.NO_RESULT);
  130. res.setKeepCallback(true);
  131. callbackContext.sendPluginResult(res);
  132. return true;
  133.  
  134. } else if (action.equals("sendBroadcast"))
  135. {
  136. if (args.length() != 1) {
  137. PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
  138. callbackContext.sendPluginResult(res);
  139. return false;
  140. }
  141.  
  142. // Parse the arguments
  143. JSONObject obj = args.getJSONObject(0);
  144.  
  145. JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
  146. Map<String,value);
  147. }
  148. }
  149.  
  150. sendBroadcast(obj.getString("action"),extrasMap);
  151. callbackContext.success();
  152. return true;
  153.  
  154. }
  155.  
  156. PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
  157. callbackContext.sendPluginResult(res);
  158. return false;
  159.  
  160. } catch (JSONException e) {
  161. callbackContext.error(e.getMessage());
  162. return false;
  163. }
  164. }
  165.  
  166. @Override
  167. public void onNewIntent(Intent intent) {
  168. if (this.onNewIntentCallback != null) {
  169. this.onNewIntentCallback.success(intent.getDataString());
  170. }
  171. }
  172.  
  173. void startActivity(String action,Uri uri,String type,Map<String,String> extras) {
  174. Intent i = (uri != null ? new Intent(action,uri) : new Intent(action));
  175.  
  176. if (type != null && uri != null) {
  177. i.setDataAndType(uri,type); //Fix the crash problem with android 2.3.6
  178. } else {
  179. if (type != null) {
  180. i.setType(type);
  181. }
  182. }
  183.  
  184. for (String key : extras.keySet()) {
  185. String value = extras.get(key);
  186. // If type is text html,the extra text must sent as HTML
  187. if (key.equals(Intent.EXTRA_TEXT) && type.equals("text/html")) {
  188. i.putExtra(key,Html.fromHtml(value));
  189. } else if (key.equals(Intent.EXTRA_STREAM)) {
  190. // allowes sharing of images as attachments.
  191. // value in this case should be a URI of a file
  192. i.putExtra(key,Uri.parse(value));
  193. } else if (key.equals(Intent.EXTRA_EMAIL)) {
  194. // allows to add the email address of the receiver
  195. i.putExtra(Intent.EXTRA_EMAIL,new String[] { value });
  196. } else {
  197. i.putExtra(key,value);
  198. }
  199. }
  200. ((DroidGap)this.cordova.getActivity()).startActivity(i);
  201. }
  202.  
  203. void sendBroadcast(String action,String> extras) {
  204. Intent intent = new Intent();
  205. intent.setAction(action);
  206. for (String key : extras.keySet()) {
  207. String value = extras.get(key);
  208. intent.putExtra(key,value);
  209. }
  210.  
  211. ((DroidGap)this.cordova.getActivity()).sendBroadcast(intent);
  212. }
  213. }

和html文件.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Intent Test</title>
  5.  
  6. <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
  7. <script src="webintent.js"></script>
  8. <script type="text/javascript" charset="utf-8">
  9.  
  10. // Wait for device API libraries to load
  11. //
  12. function onLoad() {
  13. alert("onLoad");
  14. document.addEventListener("deviceready",onDeviceReady,false);
  15. }
  16.  
  17. // device APIs are available
  18. //
  19. function onDeviceReady() {
  20. alert("onDeviceReady");
  21. window.plugins.webintent.getUri(function(url) {
  22. if(url !== "") {
  23. // url is the url the intent was launched with
  24. document.querySelector("#test").innerHTML = "URL was "+url;
  25. }
  26. });
  27. }
  28.  
  29. </script>
  30. </head>
  31. <body onload="onLoad()">
  32. <h1>Test</h1>
  33.  
  34. <div id="test"></div>
  35. </body>
  36. </html>

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

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

猜你在找的Android相关文章