Saturday, November 5, 2011

How to launch an Android App from the Browser

As i was doing some experimentation with android, i tried to launch a native app from a browser link... and i was successful... i would like to share whatever i have done with you..
The first thing one has to do to launch an app from a browser’s link is to add an additional intent filter in the app’s  Manifest file... for example to launch my native app (called UnitConverter) i have added the following few lines in the App’s manifest...

<intent-filter>
 <data android:scheme="unitconverter" />
 <action android:name="android.intent.action.VIEW" />
 <category android:name="android.intent.category.BROWSABLE" />
 <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
  
and in the web app i have added html code as 


<a href="unitconverter://">Launch UnitConverter App</a>

and it has worked fine..

However, sometimes we may need to pass some query parameters in the html query tag like


<a href="unitconverter://?param1key=param1value&param2key=param2value">Launch 
UnitConverter App</a>

To catch these query parameters inside the Android app, i have written the following piece of code and added it in the onCreate function of the main Activity just after the line super.onCreate(savedInstanceState).


//to get the parameter passed from the URI that has launched this app
     final Intent intent = getIntent();
     String scheme = intent.getScheme();
     if(scheme != null){
             final Uri myURI=intent.getData();
             String queryString = new String();
                
             if(myURI!=null)
             {
               queryString = myURI.getQuery();
             }
             String split1 = "&";
             String split2 = "=";
             
             if (queryString != null)
             {
                 final String[] arrParameters = queryString.split("&");
                 for (final String tempParameterString : arrParameters)
                 {
                     final String[] arrTempParameter = tempParameterString.split("=");
                     if (arrTempParameter.length( )== 2)
                     {
                         final String parameterKey = arrTempParameter[0];
                         final String parameterValue = arrTempParameter[1];
                         //do something with the parameters
                         Toast.makeText(this, parameterValue, 500).show();
                     }
                 }
             }    
     }

And voila!!! it has worked perfectly...


Hope this discussion comes handy for the Android learners...

3 comments:

Viktoriia said...

Hi Som.
I like you article, but have a problem with the link.
I need to send a link by email that will open my app.
So, I've done something like this

public void onClick(View v) {
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND); emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "");
Uri myUri = Uri.parse("myapp://");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html
.fromHtml("Check out this great application: Link"));

and in the manifest







and as you wrote in the method but the link doesn't work...
Could you help me, please?

Som said...

Viktoriia... see the following link...
i think you will get the answer from user1053327's post...

please try it and if it helps let me know...

http://stackoverflow.com/questions/3469908/make-a-link-in-the-android-browser-start-up-my-app

iCaption That said...

Interesting post! I enjoyed reading it!
Thanks for sharing this useful info.keep updating same way.
cheers!
iCaption That

Share