Example of Intents with PhoneGap
This weekend I was reading an email on my phone when I noticed something odd. The link, to a Wikipedia article, prompted me to ask if I wanted to load it in my browser, or in the Wikipedia app. Knowing that the Wikipedia app was built with PhoneGap, I decided to dig into this and see how it was done.
I did some research and discovered that (as far as I could tell), the Wikipedia app was making use of an Android feature called Intents. From my understanding, Intents are a way for applications to...
- Broadcast out a general request - ie, "I have an address and I'd love for someone to do something fancy with it!"
- Listen for general requests - ie, "Dude, I can so do awesome things with maps. If you have an address, let me know, cuz I'll so do something awesome with it. Awesome."
Turns out there is already a plugin for this: WebIntent. To make use of this plugin, you have to modify the Java code a bit (I forgot to bookmark it, but someone else provided the help here) to support the latest version of PhoneGap. I've included a copy with my blog entry so feel free to just copy it from there. But once you have the plugin installed, you can do either (or both) of the actions above.
Creating an intent is as simple as using a bit of JavaScript:
But making your application listen for an intent involves a bit more work, specifically, modifying your AndroidManifest.xml file. You need to add a bit of XML in this to register the intent while also using JavaScript in your application to notice when your app was launched.
Via Stackoverflow, I found this entry on listening out for Youtube links, and then added it to my AndroidManifest.xml:
And then I used a bit of JavaScript to notice the intent.
Simple, right? After installing the application, I whipped up a quick HTML page with two links. One pointing to my blog, another to a random Youtube video.

When I click the Youtube link, I now get this:

and if I select my own application, the JavaScript I wrote notices and responds to the invocation:

Pretty simple! I really barely touched on the power and reach of Intents, and I have no idea if something similar exists for iOS (surely it must), but all in all this is incredibly easy to use with PhoneGap.

WIth Android you can start an intent which is like when iOS starts another app to handle data. For instance when Dropbox fires off iBooks to handle the ePub file I just downloaded.
But you can also start an intent and wait for a result on Android. This way you can cobble together applications using already existing intents. Much in the same way you can pipe together a number of UNIX commands to make something really powerful.
I can't think of an analogy for that type of behaviour on iOS. As best I know as soon as you start a new app on iOS there is no way to pass the data back to the original app.
I see that the intent filter is registered, the youtube link/URL gets intercepted on load and passed into my app, but the url response callback does not fire with the URL the way it does in the 1.6 example.
this stackoverflow page was the clearest answer:
http://stackoverflow.com/questions/9992433/phonega...
This one is also helpful:
http://stackoverflow.com/questions/11857958/phoneg...
I am now going to move on and see if I can integrate this with gwt-phonegap! :-)
@AliBhai: You don't use an Intent to open a file, rather, you broadcast the desire to open a file and apps that have said they respond to such intents ("I can open PDFs!") will respond.
For example: i open an application that has got the "share" button, i press button and there is my app in the list.
When i click my app, it opens and tell me the EXTRA_TEXT, used to call my app.
I did something like this, but it doesnt work :(
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
console.log("Device Ready");
window.plugins.webintent.getExtra(WebIntent.EXTRA_TEXT,
function(url) {
// url is the value of EXTRA_TEXT
alert(url);
}, function() {
// There was no extra supplied.
}
);
alert("nothing"); in the
function() {
// There was no extra supplied.
}
and it everytime execute that alert, instead of alert(url).
This is in the Manifest:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:host="www.youtube.com" android:mimeType="text/*" />
</intent-filter>
First of all thank you for your help!!!!
I tried this way:
window.plugins.webintent.hasExtra(WebIntent.EXTRA_STREAM,
function(url) {
// url is the value of EXTRA_TEXT
alert("parametri");
}, function() {
// There was no extra supplied.
alert("niente");
}
with all EXTRA_stuff, but it still print "niente".
Thank you!
http://www.raymondcamden.com/index.cfm/2012/10/10/...
and note how I had to 'recognize' the data from Parse's push stuff. Specifically:
window.plugins.webintent.hasExtra("com.parse.Data"
Based on your suggestion i tried a bit of debug:
private static final String TAG = "ProvaIntent";
Bundle extras = getIntent().getExtras();
if (extras == null) {
Log.d(TAG, "error");
}else{
String value1 = extras.getString(Intent.EXTRA_TEXT);
Log.d(TAG, value1);
}
It works: when i call my program from Youtube Share link, it print the youtube link, when i call it from my computer (Run on Eclipse), it prints "error"!!
But i don't know which is the right string to catch the EXTRA_TEXT.
I tried with "android.intent.extra.TEXT" as seen on the Android Developers Guide, but it doesnt work!
So I have only to understand wich is the right string! :)
Thank you!!!!
Luca
Thank you, my friend ;)
I added the <intent-filter> with an <action> "android.intent.category", <category> "android.intent.category.DEFAULT" and "android.intent.category.BROWSABLE", added the plugin as described but nothing happens if I click on the link or android is just asking for other apps to open with and not my own.
Could it be that the plug-in isn't working with cordova/phonegap 2.2.0?
Or do you know another proper way to handle this?
In general it's good because now I can change from an url to my app but unfortunately the webIntent plug-ins method "getUri(function(url)){}" is not getting fired and the url passed is always "null".
You have any idea?
thanks, regards
yves
Want me to send you a zip of my code?
It would be nice if you could send me your code. But could you grab the url as well?
Because I could trigger my app by using the intent Filter too.
<intent-filter>
<data android:scheme="http" android:host="www.domain.com" android:pathPattern=".*" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
The important point is to add the `android:pathPattern` attribute to open your app by using the _host_ attribute together with the _scheme_ attribute.
That means, you could just use the _scheme_ attribute alone or you have to use all three:
- _scheme_
- _host_
- _path_ or _pathPattern_
Just for somebody who has the same issue.
Now I've the problem that I just get `null` returned as URI. Have you had the same issue?
There are some deprecation warnings in the _WebIntent_ Java class, maybe it could be a problem related to this warnings.
I figured out what the problem was in my case.
It's not because of the cordova version or the webintent plug-in,
but in some kind the "nfc-plugin.js"
(https://github.com/chariotsolutions/phonegap-nfc) is somehow interfering with the webintent.js. Unfortunately I need both js files, so I have to figure out what the problem exactly is.
Thanks for your time and help.
I figured out what the problem was in my case.
It's not because of the cordova version or the webintent plug-in,
but in some kind the "nfc-plugin.js"
(https://github.com/chariotsolutions/phonegap-nfc) is somehow interfering with the webintent.js. Unfortunately I need both js files, so I have to figure out what the problem exactly is.
Thanks for your time and help.
Both plugins are using the "cordova.addConstructor()" method.
"In Cordova 2.0 the addConstructor has been removed and checking for Cordova is not necessary.." - Source: http://stackoverflow.com/questions/10587398/cordov...
Cheers, yves