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.
Archived Comments
Intents are an important part of the Android application framework, so the fact that PhoneGap can utilize Intents is pretty cool.
Yeah, if you did into the PhoneGap source code you will see that Intents are used for core API calls like Camera.takePicture(), Capture.captureAudio(), Capture.captureImage() and Capture.captureVideo(). As well a number for plugins like the SpeechRecognizer and VideoPlayer also use intents to complete actions on Android.
Simon, does iOS have something similar?
Um, yes and no.
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.
Interesting. Thanks. Can iOS also register to be the receiver of an event? Your example just mentioned broadcasting out an intent. (If I read it right.)
I dont think this works with the new phongap version. Nothing is happening and I copied everything. The download you provided looks just like my file structure.
It could be that the plugin has not been updated for recent PG versions. Are you using PG 2.0?
I too cannot get it working yet with PG 2.0.0. The example as downloaded using PG 1.6.1 works as supplied, but at least my first attempt with PG 2 is not working. I'm also trying to work it in with gwt-phonegap, but since the 1.6 example works, I'm going to work through it stepwise to see what I can find out.
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.
I'd get the latest WebIntent from here (https://github.com/phonegap... and see if that helps. Be sure to read his readme.md in case the API changed.
does webintent can be used to open local pdf file packaged with android app.
Getting back to this, it does work with PhoneGap 2.0. I can post a complete solution but the last most maddening thing is that there needs to be a plugin reference in config.xml (res/xml/config.xml) for the WebIntent class, and without that you get very inscrutable class not found errors.
this stackoverflow page was the clearest answer:
http://stackoverflow.com/qu...
This one is also helpful:
http://stackoverflow.com/qu...
I am now going to move on and see if I can integrate this with gwt-phonegap! :-)
@Mikey: I'm actually doing a blog post related to this in about 20 minutes. :)
@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.
What about the send event? Is phonegap able to show the URL from a browser if your app is invoked via the "share"-list (send activity)?
Hi, im using this page to catch the text passed throught an application that call my one.
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.
}
);
How did it fail?
i putted
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>
Not sure. I'd try the hasExtra API to see if it has something at all. Maybe try some of the other EXTRAs defined in the code - https://github.com/phonegap....
im sorry but im rly noob with this things!!!
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!
I bet I know what it is. Check this blog entry:
http://www.raymondcamden.co...
and note how I had to 'recognize' the data from Parse's push stuff. Specifically:
window.plugins.webintent.hasExtra("com.parse.Data"
Thank you another time.
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!!!!
So.... you're good?
Not rally because I catched the link in my MainActivity with Java, but i can't catch it in javascript and webintent!
Luca
Not sure what to suggest then. If you want, you could send me the code and I could try here, but that crosses the line into paid support. ;)
Thank you for your help, but i'm doing it only a diversion!!!!
Thank you, my friend ;)
Unfortunately I can't get it run.
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?
Sorry the <action> is "android.intent.action.VIEW" not "android.intent.category" as written.
As far as I know this should work just fine with the latest, but I'll give it another try with 2.3.0 just to be sure.
Now I get it run but just with an fictive intent-scheme like "android-intent" and a link pointing to "href='android-intent://'" with an url like youtube or wikipedia it's bizarrely still not asking for my app.
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
No. As I said, I'm going to set up a new test w/ 2.3.0. I just haven't had time yet.
So, I can confirm me that URL filtering is no longer working. Not sure why, but looking more.
I just tried with a virgin 2.4.0 PhoneGap project and it _did_ work ok for me. When I visited a YouTube link (like in my demo above), it asked me if I wanted to use my new app.
Want me to send you a zip of my code?
Thanks Raymond.
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.
Shoot, I'm sorry, I didn't test getting the url, just the intent working. Will test in the morning.
Nice, you changed your design. Looks good.
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/chariots... 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.
Nice, you changed your design. Looks good.
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/chariots... 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.
Ok, I got it.
Both plugins are using the "cordova.addConstructor()" method.
Sorry, for my comment spamming. That will be the last one.
"In Cordova 2.0 the addConstructor has been removed and checking for Cordova is not necessary.." - Source: http://stackoverflow.com/qu...
Cheers, yves
Dude, this is not spamming at all. Thank you _so_ much for sharing this. I'd recommend filing a bug report on both plugins.
You're right. I will do that.
That old plugin is deprecated. But here is a link to a new repo that is updated to phonegap 3.x: https://github.com/InQBarna...
Thanks for sharing that, Adam.
Hi Raymond.
Thank you for this explantation. But i wonder how i can catch the Intent if a running application is brought back to front on resume. I use the following listener: document.addEventListener("resume", refreshIntentReq, false); But when i try to catch the intent from within refreshIntentReq, the shared intent is empty.
Hello all.
I am Vitaly.
I want to get the file absolute path from email attached file.
I made the phonegap app .
when tap attached file on mail, my app run successfully.
but now, I need to read the file content with my phonegap app.
so I need your help to get file path or how to read the file content?
Thanks so much
Look forward to your hearing.
Is it passed in the intent? If so - just use the File API.
thanks for your reply.
I used this plugin
https://github.com/Initsoga...
manifest to assign our app to the file extension, we have in mind:
https://www.dropbox.com/s/4...
.skt
to our app.
I wrote the basic "ondeviceready" function to see, what is in the intent parameter:
onDeviceReady: function() {
window.plugins.webintent.getUri(function(url) {
if(url !== "" && url.length > 5) {
// url is the url the intent was launched with
window.plugins.contentproviderplugin.query({
contentUri: url,
}, function (data) {
alert(JSON.stringify(data));
}, function (err) {
alert("error query");
});
}
});
the cordova plugins added were (only the first 2 are important for that):
ralfs-air:enableJS puzzler$ cordova plugin ls
com.borismus.webintent 1.0.0 "WebIntent"
com.phearme.cordovaplugin.ContentProviderPlugin 0.2.6 "ContentProviderPlugin"
de.appplant.cordova.plugin....-composer 0.8.1 "EmailComposer"
org.apache.cordova.console 0.2.8 "Console"
org.apache.cordova.device 0.2.9 "Device"
org.apache.cordova.dialogs 0.2.7 "Notification"
org.apache.cordova.inappbrowser 0.4.0 "InAppBrowser"
org.apache.cordova.splashscreen 0.3.0 "Splashscreen"
org.apache.cordova.statusbar 0.1.3 "StatusBar"
But we get the "content://blabla..." url which I can not work with... lack of brain ;-)
Regards,
Ah, well then I think you may be out of luck.
Hi.
I can get "content://com.android.email.attachmentprovider/1/32/RAW"
instead of:
/sdcard/Download/Attachments/1.skt
so I don't have any idea to read this.
I need your help for this.
Look forward to your hearing.
Regards
and I get some article from there.
http://stackoverflow.com/qu...
unfortunately, this is not phonegap, so I also stopped to work.
Thanks
I don't know - I'd recommend asking on the Google group for PhoneGap. I'm pretty busy at the moment so I can't help you with this.
I mean I need your help if you have time.
yeah, sure I will ask on the google group.
Thanks
Hi,
Now iam developing a phonegap appliaction which download some files from server. after download i want to open file with default application viewer in phone. I have downloaded all files and put it to a folder. But could not open file from my folder. I have to open different format files such as doc/xls/pdf/image etc. Could you please do help.
Thanks
What did you try? After the download I mean - what did you try to open the file?
I have tried to open that file using
window.plugins.webintent.startActivity({
action: WebIntent.ACTION_VIEW,
url:file:///sdcard/MyApp/pdffile.pdf },
function() {},
function(e) {alert('Failed to open URL via Android Intent');}
);
also tried fileopener,
window.plugins.fileOpener.open("file:///sdcard/MyApp/pdffile.pdf");
And did you see anything in the console when you tried this?
As a general FYI, in order for me to help someone, I need to know what you tried and what was reported when you did. Using remote debugging should tell you if some error was thrown.
I have started my phonegap application 1 month back. I did not try remote debugging yet. Now iam Building APK from adobe site(https://build.phonegap.com/) by uploading Zip file not by eclipse and ADT. The issue is with i could not open the file that i have downloaded with polaris viewer or any other default application based on file format.
Please test with remote debugging. If you view this site's About section you will see a list of articles I've written. There are a few articles there on that topic.
Thanks for your valuable time. I will check with remote debugging.
hi, my application need some modification now, need to open file inside application . do you have any idea to open files inside application. Is there any API for this. Please do help.
Thanks
I'd try window.location = the path. To see if that forces it to try to open.
That will open only image/pdf files. I want to view doc/xls file format also. Just like email client attachment.
Then you got me there. Not sure.
Hello,
Is it usable for WebIntent to make app phone calling strightforward? Thanks
????
thank you so much. i started looking at cordova/phonegap yesterday and got all excited but hit a wall 30 min in on how to pull in shared info into my app. I am surprised how this isn't step 2 in any of the doc's. i've been searching for the better part of 10 hours trying to find a tutorial like yours. I was about to give up and just go with copy/paste and shelve it until after..
I'm glad this almost 3 year old post is helpful. :)
Not really. If the web page was changed because of something happening on the server (imagine a web socket connection for ex), then the same thing on the server that triggered the web page change could *also* do a push notification.
Or heck - use web sockets. Looks to be supported well: http://caniuse.com/#feat=we...
Hi
First, very thanks for your good tutorial
now i want to ask, can i have this application source code?
attached files in that tutorial doesn't exist!
Fixed!
Thanks very much for the helpful tutorial. I am using PhoneGap Build and have tried a couple different WebIntent plugins, including Tunts' and InQBarna's. When I click on a link such as "www.youtube.com" I see my app listed as an option, but selecting it causes my app to immediately crash, with nothing written to the console, so remote debugging this is very hard. I am wondering if the problem might be this part of my config.xml:
activity android:name="SomeString".
I cannot figure out what is supposed to be in that name field. When I insert my app's actual name from the config.xml name field, the PhoneGap build fails. So I am just inserting an arbitrary string, which allows the build to proceed, but perhaps is leading to these crashes. Any advice would be greatly appreciated.
Sorry - you got me there. I'd try *outside* of PGBuild first.
Great tutorial. Wondering whether you have a working link to the test project you have written.
You can now find it at: http://static.raymondcamden...
I can't get it work for skype for business uri. How can i use for ms-sfb://start ?
If you are using the proper intent, than I'm not sure what to suggest outside of contacting the owner of the plugin.
Can you redo the zip file link it is broken
Soorry did not read down far enough I found it thanks.
Ray
When I add the <application> tag above to the config.xml file then attemp to generate a phonegap build the application tag cause a build error. Is there a work around?
Um - not sure. I'd check with the PG Build docs - support - etc. I haven't used PGB in years.
How can i use this tutorial with phonegap desktop build.
No idea on that one. Last time I checked I didn't think it supported plugins well.
wht the other option i can add Intent to my application
I'd use the command line.
can u suggest a tutorial so i can study the command line usage
I'd start here: http://cordova.apache.org/d...
I have a book you can buy too (grin), but it is a few years old.
can i import the current PGB project to CLI
Um - I don't believe so.
also send link to your book
https://www.amazon.com/gp/p...
is this i have to add on the config.xml
<plugin name="com.qdev.webintent" spec="https://github.com/okwei200..."/>
I honestly don't remember - this post is six years old. :) I can say plugins should be adding their own strings to config.xml as far as I know.