Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if an app is installed on an android device from within a web page

Here is the situation:

I have a web page that needs to check through JavaScript if my app is already installed on the android device it is currently running on.

If the app is installed the page will show a link (with custom protocol) to launch the app, otherwise the page should show a link to the android market.

I can manage the links to app and to market. The only remaining step is to detect the presence of the app on the device from within JavaScript code (or perhaps try to catch a possible error of unsupported protocol as an indication of not existing app).

When I

  1. click on a web link with
  2. my custom app protocol and
  3. the app is not yet installed on the device

I can see that the android environment generates an "protocol is not supported" type error. Unfortunately, I am not able to capture that error in the JavaScript code to divert the user to the android market.

I guess both direct detection and error detection are valid methods if they exist at all.

Any ideas how can I accomplish that?

Thanks for help.

like image 916
N.G. Avatar asked Jul 04 '11 06:07

N.G.


2 Answers

Don't use a custom protocol.

Instead, set up your <intent-filter> to watch for a well-known URL (e.g., set the scheme, host, and path). That URL should point to the page where your "download the app" instructions lie.

Then, if the user clicks the link to the well-known URL from someplace else, it will launch your app if installed, or will bring up your Web page if not.

like image 151
CommonsWare Avatar answered Nov 19 '22 05:11

CommonsWare


I also had the same question, but I solve it like this:

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1;
if(isAndroid) { // if is android
    // your app address for local
    var ifrSrc = 'intent://example.com#Intent;scheme=my_scheme;action=android.intent.action.VIEW;end';
    var ifr = document.createElement('iframe');
    ifr.src = ifrSrc ;
    ifr.onload = function() { // if app is not installed, then will go this function
        window.location = 'http://your.app_download_page.com';
    };
    ifr.style.display = 'none';
    document.body.appendChild(ifr);

    setTimeout(function(){
        document.body.removeChild(ifr); // remove the iframe element
    }, 1000);
} else { // if is not android
    window.location = 'http://google.com';
}

Hope this can help someone may has this problem.

like image 3
Bahramdun Adil Avatar answered Nov 19 '22 04:11

Bahramdun Adil