Home > Tricks & Tips > Detect if an application is installed or not

Detect if an application is installed or not

There will be a situation where your applications require a pre-installed application in order to use their libraries or functions, for example, sharing on Facebook or Twitter, watch video on Youtube, scanning barcode from ZXing, launch casts on Sopcast

What need to implement is to detect whether those required applications are installed or not, if not, redirect users to market and get it installed.

1. First, check if the required app is installed.

There are two ways:

+) List all installed apps and find if it is installed. (refer: Retrieve list of installed applications)

+) Fire the intent of the required app to see if it runs. (refer: Launch an activity)

Well, the first way seems to be the classical one, in case lots of apps installed on device, it might reduce performance.

The second way looks better, and if it throws an exception if app not installed,  NameNotFoundException, it is the proper time to trigger the market to install.

2. Implementation on firing Intent.

This is how I work it out:

ackage pete.apps.study.droid10;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

public class Utils {

    /** handle the required pre-installed app */
    public static boolean handleRequiredApp(Context context, PackageManager pm, String packageName)
        Intent iApp = pm.getLaunchIntentForPackage(packageName);
        try {
            // this line will trigger exception if not found
            pm.getApplicationInfo(packageName, 0);
            return true;
        } catch (Exception ex) {
            // launch the market for installation
            launchMarket(context, packageName);
            return false;

    /** launch market to certain app */
    public static void launchMarket(Context context, String packageName)
        Uri uri = Uri.parse("market://details?id=" + packageName);
        Intent iDown = new Intent(Intent.ACTION_VIEW, uri);

Note that: the Exception will fire either when application is not installed (NameNotFoundException) or installed but having no real launching Acitivity (ActivityNotFoundException). 

This is the sample project if you cannot run the code: Demo code – Droid 10


Pete Houston

Categories: Tricks & Tips Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: