How Do I Get A Phonegap App To Receive Variables On Android?
Solution 1:
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 withdocument.querySelector("#tag").innerHTML = "URL = " + url;
}
});
Edit:
Ok so I managed to get the plugin working on PhoneGap 2.9.0.
However I have to clarify some things.
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.
The problem with the lastest version of the plugin seems to be in the webintent.js file.
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){
varWebIntent = 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 = newWebIntent();
// backwards compatibilitywindow.plugins = window.plugins || {};
window.plugins.webintent = window.webintent;
})(window.PhoneGap || window.Cordova || window.cordova);
Inside WebIntent.java I changed a variable to 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
*
*/publicclassWebIntentextendsCordovaPlugin {
privateCallbackContextonNewIntentCallback=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
*/publicbooleanexecute(String action, JSONArray args, CallbackContext callbackContext) {
try {
if (action.equals("startActivity")) {
if (args.length() != 1) {
PluginResultres=newPluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
returnfalse;
}
// Parse the argumentsJSONObjectobj= args.getJSONObject(0);
Stringtype= obj.has("type") ? obj.getString("type") : null;
Uriuri= obj.has("url") ? Uri.parse(obj.getString("url")) : null;
JSONObjectextras= obj.has("extras") ? obj.getJSONObject("extras") : null;
Map<String, String> extrasMap = newHashMap<String, String>();
// Populate the extras if any existif (extras != null) {
JSONArrayextraNames= extras.names();
for (inti=0; i < extraNames.length(); i++) {
Stringkey= extraNames.getString(i);
Stringvalue= extras.getString(key);
extrasMap.put(key, value);
}
}
startActivity(obj.getString("action"), uri, type, extrasMap);
callbackContext.success();
returntrue;
} elseif (action.equals("hasExtra")) {
if (args.length() != 1) {
PluginResultres=newPluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
returnfalse;
}
Intenti= ((DroidGap)this.cordova.getActivity()).getIntent();
StringextraName= args.getString(0);
PluginResultres=newPluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
callbackContext.sendPluginResult(res);
returntrue;
} elseif (action.equals("getExtra")) {
if (args.length() != 1) {
PluginResultres=newPluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
returnfalse;
}
Intenti= ((DroidGap)this.cordova.getActivity()).getIntent();
StringextraName= args.getString(0);
if (i.hasExtra(extraName)) {
PluginResultres=newPluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
callbackContext.sendPluginResult(res);
returntrue;
} else {
PluginResultres=newPluginResult(PluginResult.Status.ERROR);
callbackContext.sendPluginResult(res);
returnfalse;
}
} elseif (action.equals("getUri")) {
if (args.length() != 0) {
PluginResultres=newPluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
returnfalse;
}
Intenti= ((DroidGap)this.cordova.getActivity()).getIntent();
Stringuri= i.getDataString();
callbackContext.success(uri);
returntrue;
} elseif (action.equals("onNewIntent")) {
if (args.length() != 0) {
PluginResultres=newPluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
returnfalse;
}
this.onNewIntentCallback = callbackContext;
PluginResultres=newPluginResult(PluginResult.Status.NO_RESULT);
res.setKeepCallback(true);
callbackContext.sendPluginResult(res);
returntrue;
} elseif (action.equals("sendBroadcast"))
{
if (args.length() != 1) {
PluginResultres=newPluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
returnfalse;
}
// Parse the argumentsJSONObjectobj= args.getJSONObject(0);
JSONObjectextras= obj.has("extras") ? obj.getJSONObject("extras") : null;
Map<String, String> extrasMap = newHashMap<String, String>();
// Populate the extras if any existif (extras != null) {
JSONArrayextraNames= extras.names();
for (inti=0; i < extraNames.length(); i++) {
Stringkey= extraNames.getString(i);
Stringvalue= extras.getString(key);
extrasMap.put(key, value);
}
}
sendBroadcast(obj.getString("action"), extrasMap);
callbackContext.success();
returntrue;
}
PluginResultres=newPluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
returnfalse;
} catch (JSONException e) {
callbackContext.error(e.getMessage());
returnfalse;
}
}
@OverridepublicvoidonNewIntent(Intent intent) {
if (this.onNewIntentCallback != null) {
this.onNewIntentCallback.success(intent.getDataString());
}
}
voidstartActivity(String action, Uri uri, String type, Map<String, String> extras) {
Intenti= (uri != null ? newIntent(action, uri) : newIntent(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()) {
Stringvalue= extras.get(key);
// If type is text html, the extra text must sent as HTMLif (key.equals(Intent.EXTRA_TEXT) && type.equals("text/html")) {
i.putExtra(key, Html.fromHtml(value));
} elseif (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));
} elseif (key.equals(Intent.EXTRA_EMAIL)) {
// allows to add the email address of the receiver
i.putExtra(Intent.EXTRA_EMAIL, newString[] { value });
} else {
i.putExtra(key, value);
}
}
((DroidGap)this.cordova.getActivity()).startActivity(i);
}
voidsendBroadcast(String action, Map<String, String> extras) {
Intentintent=newIntent();
intent.setAction(action);
for (String key : extras.keySet()) {
Stringvalue= extras.get(key);
intent.putExtra(key, value);
}
((DroidGap)this.cordova.getActivity()).sendBroadcast(intent);
}
}
And the html file.
<!DOCTYPE html><html><head><title>Intent Test</title><scripttype="text/javascript"charset="utf-8"src="cordova.js"></script><scriptsrc="webintent.js"></script><scripttype="text/javascript"charset="utf-8">// Wait for device API libraries to load//functiononLoad() {
alert("onLoad");
document.addEventListener("deviceready", onDeviceReady, false);
}
// device APIs are available//functiononDeviceReady() {
alert("onDeviceReady");
window.plugins.webintent.getUri(function(url) {
if(url !== "") {
// url is the url the intent was launched withdocument.querySelector("#test").innerHTML = "URL was "+url;
}
});
}
</script></head><bodyonload="onLoad()"><h1>Test</h1><divid="test"></div></body></html>
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.
Update 3.0: https://github.com/Initsogar/cordova-webintent
Post a Comment for "How Do I Get A Phonegap App To Receive Variables On Android?"