How can I find if a particular package exists on my Android device?
Asked Answered
M

10

77

How can I find whether a particular package or application, say: com.android.abc, exists on my Android device?

Marlinemarlinespike answered 20/7, 2011 at 8:7 Comment(0)
L
161

Call any of the below method with the package name.

import android.content.pm.PackageManager;

// ...

    public boolean isPackageExisted(String targetPackage){
        List<ApplicationInfo> packages;
        PackageManager pm;

        pm = getPackageManager();        
        packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
            if(packageInfo.packageName.equals(targetPackage))
                return true;
        }
        return false;
    }

 import android.content.pm.PackageManager;

 public boolean isPackageExisted(String targetPackage){
   PackageManager pm=getPackageManager();
   try {
     PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
   } catch (PackageManager.NameNotFoundException e) {
     return false;
   }  
   return true;
 }
Landing answered 20/7, 2011 at 8:18 Comment(5)
Thanks Rasel, I was looking for this only. Thank U very much.Marlinemarlinespike
What do I have to import to get this to work? I get cannot resolve symbol 'PackageManager' error.Yahrzeit
the second method is fasterPrecipitant
Does this require any permission? i am still getting false!Leeward
@EmonHossainMunna depending on the Android version, you now need to declare the packages you interact with in the manifest: developer.android.com/guide/topics/manifest/queries-elementMorphosis
S
14

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.size() > 0;
}
Stuppy answered 8/6, 2015 at 11:40 Comment(0)
G
11

Kotlin

fun isPackageExist(context: Context, target: String): Boolean {
     return context.packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null
}

Edit: Extension Function

fun Context.isPackageExist(target: String): Boolean {
     return packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null
}
Gerrilee answered 7/3, 2019 at 14:7 Comment(0)
C
4
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
Carnap answered 7/12, 2013 at 23:57 Comment(0)
K
4

We can check like this:

 if(getPackageManager().hasSystemFeature("android.software.webview") == true && isPackageExisted("com.google.android.webview")) {
            if (Constant.isNetworkConnected(Activity.this)) {
                 //Your Intent 
            } else {
                Toast.makeText(getApplicationContext(), resources.getString(R.string.internet_error), Toast.LENGTH_SHORT).show();
            }
        }   else
            {
                Constant.showDialog(Activity.this,"Please install the webview");
            }
    }

Make method for package check ! this credit goes to "Kavi" https://mcmap.net/q/264609/-how-can-i-find-if-a-particular-package-exists-on-my-android-device

public boolean isPackageExisted(String targetPackage) {
    List<ApplicationInfo> packages;
    PackageManager pm;

    pm = getPackageManager();
    packages = pm.getInstalledApplications(0);
    for (ApplicationInfo packageInfo : packages) {
        if(packageInfo.packageName.equals(targetPackage))
        {
            return true;
        }
}
return false;

}

Kailey answered 29/11, 2019 at 10:30 Comment(0)
A
3

You should use PackageManager's function called getInstalledPackages() to get the list of all installed packages and the search for the one you are interested in. Note that package name is located in PackageInfo.packageName field.

Anschluss answered 20/7, 2011 at 8:15 Comment(0)
A
2

Since some devices have reported that the "getInstalledPackages" can cause TransactionTooLargeException (check here, here and here), I think you should also have a fallback like I did below.

This issue was supposed to be fixed on Android 5.1 (read here), but some still reported about it.

public static List<String> getInstalledPackages(final Context context) {
    List<String> result = new ArrayList<>();
    final PackageManager pm = context.getPackageManager();
    try {
        List<PackageInfo> apps = pm.getInstalledPackages(0);
        for (PackageInfo packageInfo : apps)
            result.add(packageInfo.packageName);
        return result;
    } catch (Exception ignored) {
        //we don't care why it didn't succeed. We'll do it using an alternative way instead
    }
    // use fallback:
    BufferedReader bufferedReader = null;
    try {
        Process process = Runtime.getRuntime().exec("pm list packages");
        bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            final String packageName = line.substring(line.indexOf(':') + 1);
            result.add(packageName);
        }
        closeQuietly(bufferedReader);
        process.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeQuietly(bufferedReader);
    }
    return result;
}

public static void closeQuietly(final Closeable closeable) {
    if (closeable == null)
        return;
    try {
        closeable.close();
    } catch (final IOException e) {
    }
}
Augury answered 21/3, 2016 at 7:52 Comment(0)
B
2

If you just want to use adb:

adb shell "pm list packages"|cut -f 2 -d ":"

it will list all installed packages.

Bug answered 23/5, 2016 at 11:2 Comment(0)
M
2

You can use pm.getPackageUid() instead of iterating over the pm.getInstalledApplications()

 boolean isPackageInstalled;
 PackageManager pm = getPackageManager();   
 int flags = 0; 
        try 
        {   
              pm.getPackageUid(packageName,flags);               
              isPackageInstalled = true;    
        }   
        catch (final PackageManager.NameNotFoundException nnfe) 
        {   
            isPackageInstalled = false; 
        }                   
 return isPackageInstalled;
Mcginty answered 5/10, 2018 at 16:20 Comment(1)
getPackageUid - Added in API level 24, though.Dove
T
0

According to the Package visibility filtering changes in Android 11, you need to add this permission to your manifest to be able to list installed apps:

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

but Google doesn't recommend to use this way. You should use <queries> tag instead:

<manifest ...>
    <queries>
        <package android:name="com.app.package" />
        ...
    </queries>
    ...
</manifest>

And in your code:

fun isAppInstalled(context: Context, packageId: String): Boolean {
    return try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            context.packageManager
                .getApplicationInfo(packageId, PackageManager.ApplicationInfoFlags.of(0))
        } else {
            context.packageManager.getApplicationInfo(packageId, 0)
        }
        true
    } catch (e: PackageManager.NameNotFoundException) {
        false
    }
}
Twelfth answered 26/12, 2022 at 22:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.