Jeff
Jeff

Reputation: 1403

Detect an application is installed or not?

I have a application that registers a handler for custom URI on the Phone. I am able to launch the application by making a link to "myapp://act/launch" from my phone web pages. This works only if my application is installed on the device. What I want to do is detect if the URI Scheme is supported from the browser and then prompt my own message saying "please download the app etc..." if the handler for the URI scheme is not found.

Is there a way I can detect or find the list of URL Scheme handlers on the Phone from the Web Browser ?

Upvotes: 25

Views: 43914

Answers (4)

Asad Ali Choudhry
Asad Ali Choudhry

Reputation: 5261

Although the accepted answer is correct, but those who are looking for Kotlin solution can use this method,

Here I have written an article. Check If Application is Installed in Android Kotlin

fun isAppInstalled(packageName: String, context: Context): Boolean {
        return try {
            val packageManager = context.packageManager
            packageManager.getPackageInfo(packageName, 0)
            true
        } catch (e: PackageManager.NameNotFoundException) {
            false
        }
    }

Upvotes: 0

Waqar UlHaq
Waqar UlHaq

Reputation: 6422

In my custom Helper.java

public static boolean isPackageInstalled(Context context, String packageName)
{
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        Log.d("Tag",packageName + " not installed");
    }
    return false;
}

Call it by

Helper.isPackageInstalled(yourActivity.this,"app_package_name")

Upvotes: 1

boatcoder
boatcoder

Reputation: 18107

  1. You need to create a web page that user lands on if the app is not installed. Say http://www.yourcompany.com/android/android_app_is_not_installed_landing_page.html

  2. The app needs to register in manifest XML file host www.yourcompany.com, schema “http” and the path param to app not install landing page in the manifest intent filter:

    <!-- These intents are used to launch the app from the web page-->
    <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="http"
            android:host="www.yourcompany.com"
            android:path="/android/android_app_is_not_installed_landing_page.html" 
      />
    </intent-filter>
    
  3. Now in the web page that invokes your app you specify full link to android_app_is_not_installed_landing_page.html followed by whatever params you want to pass to the app:

http://www.yourcompany.com/android/ android_app_is_not_installed_landing_page.html? '>Click here to launch the app if it is installed

If the app is installed it will be launched and passed entire http://www.yourcompany.com/android/android_app_is_not_installed_landing_page.html?' in intent that it needs to parse, extract the and do whatever it is supposed to do.

If the app is not installed, then the Android Web browser will open “Android app not installed” landing page http://www.yourcompany.com/android/android_app_is_not_installed_landing_page.html o . That page should message user to install the app and provide the link to install it from Android marketplace.

found this write up here: https://groups.google.com/forum/m/?fromgroups#!topic/android-developers/RGLHkGUpRoA

Upvotes: 15

Jaime M.
Jaime M.

Reputation: 3813

From Android How-to's

If you ever need to know if a particular app is installed on the user's device, you can use the PackageManager. From a Context class (e.g. an Activity or a Service) you can call getPackageManager(). This gives you a variety of methods, one of which is getPackageInfo(). Below is a method you might use. You might call it like this:

isAppInstalled("com.simexusa.campusmaps_full");

private boolean isAppInstalled(String packageName) {
    PackageManager pm = getPackageManager();
    boolean installed = false;
    try {
       pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
       installed = true;
    } catch (PackageManager.NameNotFoundException e) {
       installed = false;
    }
    return installed;
}

Upvotes: 70

Related Questions