How to check programmatically if an application is installed or not in Android?

AndroidApk

Android Problem Overview


We have installed applications programmatically.

  1. If the application is already installed in the device the application is open automatically.
  2. Otherwise install the particular application.

Guide Me. I have no idea. Thanks.

Android Solutions


Solution 1 - Android

Try with this:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Add respective layout
        setContentView(R.layout.main_activity);

        // Use package name which we want to check
        boolean isAppInstalled = appInstalledOrNot("com.check.application");  
        
        if(isAppInstalled) {
            //This intent will help you to launch if the package is already installed
			Intent LaunchIntent = getPackageManager()
				.getLaunchIntentForPackage("com.check.application");
            startActivity(LaunchIntent);
					
            Log.i("SampleLog", "Application is already installed.");		  
        } else {
            // Do whatever we want to do if application not installed
            // For example, Redirect to play store

            Log.i("SampleLog", "Application is not currently installed.");
        }
    }

    private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
        }

        return false;
    }

}

Solution 2 - Android

Somewhat cleaner solution than the accepted answer (based on this question):

public static boolean isAppInstalled(Context context, String packageName) {
    try {
        context.getPackageManager().getApplicationInfo(packageName, 0);
        return true;
    }
    catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

I chose to put it in a helper class as a static utility. Usage example:

boolean whatsappFound = AndroidUtils.isAppInstalled(context, "com.whatsapp");

This answer shows how to get the app from the Play Store if the app is missing, though care needs to be taken on devices that don't have the Play Store.

Solution 3 - Android

The above code didn't work for me. The following approach worked.

Create an Intent object with appropriate info and then check if the Intent is callable or not using the following function:

private boolean isCallable(Intent intent) {  
        List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,   
        PackageManager.MATCH_DEFAULT_ONLY);  
        return list.size() > 0;  
}

Solution 4 - Android

If you know the package name, then this works without using a try-catch block or iterating through a bunch of packages:

public static boolean isPackageInstalled(Context context, String packageName) {
    final PackageManager packageManager = context.getPackageManager();
    Intent intent = packageManager.getLaunchIntentForPackage(packageName);
    if (intent == null) {
        return false;
    }
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return !list.isEmpty();
}

Solution 5 - Android

This code checks to make sure the app is installed, but also checks to make sure it's enabled.

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

Solution 6 - Android

>Check App is installed or not in Android by using kotlin.

Creating kotlin extension.

fun PackageManager.isAppInstalled(packageName: String): Boolean = try {
        getApplicationInfo(packageName, PackageManager.GET_META_DATA)
        true
    } catch (e: Exception) {
        false
    }

Now, can check if app is install or not

if (packageManager.isAppInstalled("AppPackageName")) {
    // App is installed
}else{
    // App is not installed
}

Solution 7 - Android

A simpler implementation using Kotlin

fun PackageManager.isAppInstalled(packageName: String): Boolean =
        getInstalledApplications(PackageManager.GET_META_DATA)
                .firstOrNull { it.packageName == packageName } != null

And call it like this (seeking for Spotify app):

packageManager.isAppInstalled("com.spotify.music")

Solution 8 - Android

Cleaner solution (without try-catch) than the accepted answer (based on AndroidRate Library):

public static boolean isPackageExists(@NonNull final Context context, @NonNull final String targetPackage) {
    List<ApplicationInfo> packages = context.getPackageManager().getInstalledApplications(0);
    for (ApplicationInfo packageInfo : packages) {
        if (targetPackage.equals(packageInfo.packageName)) {
            return true;
        }
    }
    return false;
}

Solution 9 - Android

I think using try/catch pattern is not very well for performance. I advice to use this:

public static boolean appInstalledOrNot(Context context, String uri) {
    PackageManager pm = context.getPackageManager();
    List<PackageInfo> packageInfoList = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES);
    if (packageInfoList != null) {
        for (PackageInfo packageInfo : packageInfoList) {
            String packageName = packageInfo.packageName;
            if (packageName != null && packageName.equals(uri)) {
                return true;
            }
        }
    }
    return false;
}

Solution 10 - Android

Try this

> This code is used to check weather your application with package name is installed or > not if not then it will open playstore link of your app otherwise your > installed app

String your_apppackagename="com.app.testing";
    PackageManager packageManager = getPackageManager();
    ApplicationInfo applicationInfo = null;
    try {
        applicationInfo = packageManager.getApplicationInfo(your_apppackagename, 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    if (applicationInfo == null) {
        // not installed it will open your app directly on playstore
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + your_apppackagename)));
    } else {
        // Installed
        Intent LaunchIntent = packageManager.getLaunchIntentForPackage(your_apppackagename);
        startActivity( LaunchIntent );
    }

Solution 11 - Android

All the answers only check certain app is installed or not. But, as we all know an app can be installed but disabled by the user, unusable.

Therefore, this solution checks for both. i.e, installed AND enabled apps.

public static boolean isPackageInstalled(String packageName, PackageManager packageManager) {
     try {
          return packageManager.getApplicationInfo(packageName, 0).enabled;
     }
     catch (PackageManager.NameNotFoundException e) {
          return false;
     }
}

Call the method isPackageInstalled():

boolean isAppInstalled = isPackageInstalled("com.android.app" , this.getPackageManager());

Now, use the boolean variable isAppInstalled and do whatever you want.

if(isAppInstalled ) {
    /* do whatever you want */
}

Solution 12 - Android

Android 11 update
You have to specify in the manifest the exact bundle id's you want to search for.

Example for facebook and whatsapp:

Inside the Manifest above "application" (where the permissions are)

<queries>
    <package android:name="com.whatsapp" />
    <package android:name="com.facebook.katana" />
</queries>  

This will allow you to check if facebook and whatsapp are installed, otherwise you will always get false for that check.

Further reading on the subject:
https://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9

Solution 13 - Android

@Egemen Hamutçu s answer in kotlin B-)

    private fun isAppInstalled(context: Context, uri: String): Boolean {
        val packageInfoList = context.packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES)
        return packageInfoList.asSequence().filter { it?.packageName == uri }.any()
    }

Solution 14 - Android

A cool answer to other problems. If you do not want to differentiate "com.myapp.debug" and "com.myapp.release" for example !

public static boolean isAppInstalled(final Context context, final String packageName) {
    final List<ApplicationInfo> appsInfo = context.getPackageManager().getInstalledApplications(0);
    for (final ApplicationInfo appInfo : appsInfo) {
        if (appInfo.packageName.contains(packageName)) return true;
    }
    return false;
}

Solution 15 - Android

So nicer with Kotlin suger:

  private fun isSomePackageInstalled(context: Context, packageName: String): Boolean {

    val packageManager = context.packageManager

    return runCatching { packageManager.getPackageInfo(packageName, 0) }.isSuccess
  }

Solution 16 - Android

You can do it using Kotlin extensions :

fun Context.getInstalledPackages(): List<String> {
    val packagesList = mutableListOf<String>()
    packageManager.getInstalledPackages(0).forEach {
        if ( it.applicationInfo.sourceDir.startsWith("/data/app/") && it.versionName != null)
            packagesList.add(it.packageName)
    }
    return packagesList
}

fun Context.isInDevice(packageName: String): Boolean {
    return getInstalledPackages().contains(packageName)
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSathish SathishView Question on Stackoverflow
Solution 1 - AndroidAerrowView Answer on Stackoverflow
Solution 2 - AndroidJonikView Answer on Stackoverflow
Solution 3 - AndroidPriyank DesaiView Answer on Stackoverflow
Solution 4 - AndroidKaviView Answer on Stackoverflow
Solution 5 - Androidcaly__psoView Answer on Stackoverflow
Solution 6 - AndroidNiral DhameliyaView Answer on Stackoverflow
Solution 7 - AndroidVíctor AlbertosView Answer on Stackoverflow
Solution 8 - AndroidAlexander SavinView Answer on Stackoverflow
Solution 9 - AndroidEgemen HamutçuView Answer on Stackoverflow
Solution 10 - AndroidSunilView Answer on Stackoverflow
Solution 11 - AndroidAashish KumarView Answer on Stackoverflow
Solution 12 - AndroidMark KazakovView Answer on Stackoverflow
Solution 13 - AndroidKishan BView Answer on Stackoverflow
Solution 14 - AndroidNicolas DuponchelView Answer on Stackoverflow
Solution 15 - AndroidDavidView Answer on Stackoverflow
Solution 16 - AndroidJéwôm'View Answer on Stackoverflow