Posts Tagged ‘pre-installed’

Detect if an application is installed or not

January 3, 2013 Leave a comment

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:


import android.content.Context;
import android.content.Intent;

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: ,