Handling PhoneGap Android Application Using Custom URL Scheme

Introduction:-
When we are launching the Android application using custom URL scheme, we may need to extract the parameter attached with the custom URL that we are using as link in the other native application like email.

Description:-
In my last post I have described how to launch the android application using custom scheme. So continuing with that here we can get to know how to use the same URL scheme along with WebIntent Plugin to get the parameter attached with the custom URL.

After creating the intent in the manifest file in the next step we need to pass the  parameter via the href in the anchor tag. Lets say we are passing the user name as parameter in the href along with the custom scheme and we need the same user name after launching the application.

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category  android:name="android.intent.category.DEFAULT" />
<category  android:name="android.intent.category.BROWSABLE" />
<data android:scheme="mytestapplication"  android:host="*"/>
</intent-filter>

This is the intent with the custom url scheme as “mytestapplication”.

<a href="mytestapplication://customurl/username=James Bond">Launch Application</a>

And this is the custom URL which contains the scheme “mytestapplication” along with the user name.

So in the next step we need a plugin which will interact with the intent created in Manifest and help us to get the custom URL. For this there is a plugin called WebIntent already available for PhoneGap . So we just need to follow the steps bellow to integrate the WebIntent plugin with our application.

1. Download the WebIntent PhoneGap plugin for Android from here https://github.com/phonegap/phonegap-plugins/tree/DEPRECATED/Android/WebIntent

2. Copy the WebIntent.java file and put it inside the src hierarchy of your project.

3. Change the package name with the package name of your project at the top of the WebIntent.java file. In my case I put it inside src/com/phonegap/demo and renamed the package name in java file from com.borismus.webintent to com.phonegap.demo.webintent.

4. Add the js for phonegap (phonegap/cordova.js) in your HTML page.

5. Copy the webintent.js file, put it inside the assets/www folder of your project and refer it in header section of your html page.

6. Add the reference for the plugin in res/xml/config.xml file.

<plugin name=”WebIntent” value=”com.phonegap.demo.WebIntent” />

7. Now create a function to get the custom URL when app launches using custom URL.


function GetCustomUrl() {
window.plugins.webintent.getUri(function(url) {
if(url !== "") {
//Here we can parse the url .
var link = url.split('=');
var username = link[1];
alert(username);
}
});
}

8. Add the event listener to load the function when device gets ready.
document.addEventListener(“deviceready”, GetCustomUrl, false);

Summary:-
So using the above example we can handle the custom URL scheme and also get the  parameter attached with custom URL after launching the application.

Written By: Sourabha Kumar Sahoo, Software Developer, Mindfire Solutions

Advertisements

3 thoughts on “Handling PhoneGap Android Application Using Custom URL Scheme

  1. Hey! I understand this is sort of off-topic but I needed to ask.
    Does operating a well-established blog such
    as yours require a lot of work? I am brand new to writing a blog however I
    do write in my diary on a daily basis. I’d like to start a blog so I
    can easily share my personal experience and feelings online.

    Please let me know if you have any ideas or tips for brand new aspiring blog owners.
    Thankyou!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s