46

I'm looking for a way to find out if a specific application is installed from a client-side web browser. The platform is Android.

For example I write my own web site and I write my own application, now I want to when user comes to my own site from Android phone Browser. The Browser look if the application is already installed on the phone and if not suggest to install application. Can I do that ?

Itay Kahana
  • 2,355
  • 1
  • 26
  • 20

5 Answers5

125

There is a way to achieve this.

You cannot detect if a particular application is installed, for security and privacy reasons. But you can do a trick to open the app if it's installed or open its Google Play page if it isn't.

To do that, you must create an intent-filter on your app's main activity, to open it when a given URL is called. Like this:

    <activity android:name=".MainActivity >
        <intent-filter>
            <data
                android:host="www.myurl.com"
                android:pathPrefix="/openmyapp"
                android:scheme="http" >
            </data>

            <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" />
        </intent-filter>
    </activity> 

Explaining: when the user navigates to http://www.myurl.com/openmyapp, if the app is installed, an intent will be created and the Activity will be shown.

But what if the user doesn't have the app installed? Then you need to create a redirect page on your http://www.myurl.com/openmyapp/index.html. When the user reaches this address, your server must redirect to market://details?id=com.your.app.package.

This way, when no Intent is created after the user navigates to http://www.myurl.com/openmyapp, the webserver will call another URL. That URL, in turn, will open Google Play on the device, directly on the app's page.

starball
  • 20,030
  • 7
  • 43
  • 238
leocadiotine
  • 3,295
  • 2
  • 22
  • 19
  • 1
    that is a very interesting idea, however when the link to the intent is clicked, the user is presented with a dialog to choose between the browser and the app - any way around this ? – Nick Andriopoulos May 09 '13 at 14:42
  • 1
    Good call. The user has to choose the app and then select "always" for the intent filter to work on the first time. Next time, Android won't ask anymore. This is Android's default behaviour. The exact same thing happen when you click on a map link, for example. Unfortunately, I don't know any way to bypass this, and I doubt there is one… – leocadiotine May 09 '13 at 18:57
  • 13
    @leocadiotine I think there is a way to bypass the "choose app" with guideline in Chrome of Google (https://developers.google.com/chrome/mobile/docs/intents). In summary, a link with such format should be called from browser ** Force open by package_name app **. The app with package name as is searched and open, otherwise, it is forwarded to GooglePlay search result. – Nezneika Aug 14 '13 at 04:20
  • Interesting, @hami! I din't know that, thanks for sharing. The only thing is that it seems that it'll only work with Google browsers (Chrome and the Android browser)… – leocadiotine Aug 14 '13 at 20:43
  • @leocadiotine I tested on Chrome and Android default browser on some devices with 4.0 and over. They actually work. Hope someone finds it useful :) – Nezneika Aug 16 '13 at 06:10
  • Great! Can you test that on a 3rd party browser (such as Firefox or Dolphin)? – leocadiotine Aug 16 '13 at 17:37
  • 1
    It seems to work with dolphin too (it also uses webkit engine just a note). – Igor Čordaš Mar 25 '14 at 11:26
  • 7
    The link mentioned by Nezneika is now http://developer.chrome.com/multidevice/android/intents And here is a cleaned-up version of the url: Anchor text In my testing, this works on both Chrome and Opera. – Jo Jo Mar 30 '15 at 22:15
  • Working on firefox as well. Thanks – EdgarK Dec 19 '18 at 17:52
  • Is there an equivalent href when on iOS? I found this: https://stackoverflow.com/questions/52128406/html-open-ios-application-by-href-link – Ronen Rabinovici Oct 19 '21 at 07:42
  • A simpler link format rather than constructing the intent href would simply be: href="your_app_name://some_url" and registering an intent with the scheme: "your_app_name". This will bypass the 'choose app to open' dialog, as only your app is registered to the scheme "your_app_name". See more here: https://developer.android.com/training/app-links/deep-linking#adding-filters – Ronen Rabinovici Nov 03 '21 at 11:14
19

You mean from JavaScript running in the browser? I think (hope) that's impossible. I wouldn't want any random website to be able to see what apps are installed.

If you want the user to install a particular app, you can provide a Market link on your website: http://developer.android.com/guide/publishing/publishing.html#marketintent

Edit: After your clarification in the comments to my answer, a more useful answer appeared below, which rightfully has more upvotes.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • 2
    @Thomas really there is no way to know if application is installed ? Foe example I write my own web site and I write my own application, now I want when user come to my own site from Android phone Browser, Browser look if application is installed on the phone and if not suggest to install application. Can I do that ? – Viktor Apoyan Sep 14 '12 at 13:32
  • I edited that into the main question, because it's quite important. – Thomas Jan 07 '15 at 15:52
17

I have found a more useful solution. Below is the android config file:

<activity android:name="me.test.html.MainActivity" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="xxxxx"
            android:scheme="mm" />
    </intent-filter>
</activity>

Below is the html code:

<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Insert title here</title>  
<script type="text/javascript">
function test2(){
    var di = document.getElementById("di");
    di.innerHTML = "app have not installed";
}
function newOpen(){//184 064 323 438
    var di = document.getElementById("di");
    di.innerHTML = "app have installed";
    var ifc = document.getElementById("ifc");
    ifc.innerHTML = "<iframe src='mm://xxxxx?a=b&c=d' onload='test2()'></iframe>";
    return false;
}
</script>
</head>  
<body>  
 <a href="#" onclick="return newOpen()">local3</a><br/> 
<div id="di"></div> 
 <div style="display:none;" id="ifc"></div>
</body>  
</html>

In this way, when the user clicks the tag, and if the device has the app installed, then it will not show a dialog to let the user choose, but instead open the app directly; and if the app hasn't been installed, then the js function "test2" will be called, therefore we know that the app has not been installed, so we can do anything in the "test2"! The benefit is that we don't need to use the standard html schema, which would show a choose dialog, and if I use the defined schema myself, the page would not been navigated to a wrong page! I am a Chinese, my English is not good, hope everyone could understand me and let's others know the resolution.

btx9000
  • 438
  • 4
  • 10
HaiYu
  • 171
  • 1
  • 2
5

You could make use of Android Intents. You can construct an intent anchor to launch the app. If the app is not installed then the browser searches for the package name specified in the intent anchor. We can provide a Fall Back URL as well.

example:

<a href="intent://scan/#Intent;scheme=zxing package=com.google.zxing.client.android;end"> Take a QR code </a>

This is not supported in all the browsers. Chrome and few other major browsers support it. Check this link for more detail.

I know this is late thought it might help someone.

Ram Chandran
  • 191
  • 1
  • 4
  • 16
5

This might be available soon publicly.

Chrome Team says:

In Chrome 59 we are introducing a new API called getInstalledRelatedApps(). This new API lets you determine if your native app is installed on a device. (https://developers.google.com/web/updates/2017/04/getinstalledrelatedapps)

Yet:

Note: This API is behind an Origin Trial, this means that we are in an experimental mode and are actively looking for feedback. You have to opt your site into this trial (https://github.com/jpchase/OriginTrials/blob/gh-pages/developer-guide.md) because it is not available broadly on the web. You can now sign up for the trial (https://docs.google.com/forms/d/e/1FAIpQLSfO0_ptFl8r8G0UFhT0xhV17eabG-erUWBDiKSRDTqEZ_9ULQ/viewform?entry.1999497328=getInstalledRelatedApps).

Current platform status of this feature: https://www.chromestatus.com/feature/5695378309513216

Issue in bugtracker for this feature: https://bugs.chromium.org/p/chromium/issues/detail?id=587623

/Update 15.02.2018:

The Trial for getInstalledRelatedApps() has been completed: https://github.com/GoogleChrome/OriginTrials/blob/gh-pages/completed-trials.md: First available in Chrome M59. Trial ends on January 30, 2018., but it's not yet implemented in stable Chrome: Feature team currently considering the next steps for the feature.

derFunk
  • 1,587
  • 2
  • 20
  • 31