Wednesday, March 26, 2014

Launch Android App from Web Link in Browser

Most app have its official website, and you may want users to install or open your app if it is already installed when they visit the website. Here is a common solution using the intent-filter with http scheme.
In your android app, add this intent-filter in your AndroidManifest.xml so it can intercept HTTP URL and launch your app.
<activity android:name="MyMainActivity" android:label="@string/app_name" android:launchmode="singleTask" android:screenorientation="portrait" android:windowsoftinputmode="adjustPan">
 <intent-filter>
  <action android:name="android.intent.action.MAIN"/>
  <category android:name="android.intent.category.LAUNCHER"/>
 </intent-filter>
 <intent-filter>
  <action android:name="android.intent.action.VIEW"/>
  <category android:name="android.intent.category.DEFAULT"/>
  <category android:name="android.intent.category.BROWSABLE"/>
  <category android:name="android.intent.category.LAUNCHER"/>
  <data android:scheme="http" android:host="myapp.com" android:path="download"/>
 </intent-filter>
</activity>
In this example, when you click a link with URL "http://myapp.com/download", a dialog will popup and let you choose to open it with the browser or your app if the app with this intent-filter is installed. If the app is not installed, the link will open in the browser by default.
But you might want it to be more intelligent that if the app is installed, it launches the app directly without popup dialog. To achieve this goal, you can change the http scheme and use a custom scheme that will not be caught by other apps, and give the link your custom scheme URL (e.g. myUniqueScheme://myapp). 
<data android:scheme="myUniqueScheme" android:host="myapp"/>
However, this could cause problem when the app is not installed, it cannot redirect user to the download webpage. After a lot of search, finally I find a solution from stackoverflow regarding how to handle custom url scheme. However, the solution provided couldn't work on Chrome browser. And I make some changes by using intent for Chrome browser, so it can open the Play Store and search your app if it is not installed. In the javascript webpage, add following function:
function openOrDownloadURL() {
 var AppURL = "myUniqueScheme://myapp";
 var DownloadURL = "http://myapp.com/download";

 if (navigator.userAgent.match(/Android/)) {

  if (navigator.userAgent.match(/Chrome/)) {
   window.location = "intent://myapp/#Intent;scheme=myUniqueScheme;package=com.myapp;end"

  } else {
   var iframe = document.createElement("iframe");
   iframe.style.border = "none";
   iframe.style.width = "1px";
   iframe.style.height = "1px";
   iframe.onload = function() {
    window.location = DownloadURL;
   };
   iframe.src = AppURL;
   document.body.appendChild(iframe);
  }

 } else {
  // Not Android mobile
  window.location = DownloadURL;
 }
}
For hyperlink, change the url to javascript:
<a href="javascript:openOrDownloadURL();"> Open App </a>
Now in both Chrome and Android browser by clicking the link, the app can be launched directly if it is installed. If the app is not installed, in the Android browser, it will redirect to the given download url; in the Chrome browser, it will launch Play Store and search the app by package name.

4 comments:

  1. Hi, when I try to load it through UC Browser, its not working...

    ReplyDelete
  2. Indian Browser is 1st Made in India browser developed by patriot Indian Company in supporting Make in India movement. So please support Indian Browser at maximum level. Indian Browser is a 1st full fledge browser in its kind.

    ReplyDelete
  3. It work for me , thank you veryyyyyy much :)

    ReplyDelete
  4. flipboard only launched its Android app recently. The app allows users to browse through their Facebook, Twitter and Google+ streams, as well as Google Reader feeds in a beautiful flipping manner. It also consolidates and curates a stream of important stories for those who are too busy to go through everything. As a blogger, using this app and browsing through interesting news content daily will give you fresh ideas on what to write about in your next blog post.

    ReplyDelete