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

僤鯓⒐⒋嵵緔 提交于 2019-11-26 03:53:57

问题


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


回答1:


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;
 }



回答2:


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;
}



回答3:


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;



回答4:


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.




回答5:


Kotlin

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



回答6:


If you just want to use adb:

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

it will list all installed packages.




回答7:


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) {
    }
}



回答8:


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;


来源:https://stackoverflow.com/questions/6758841/how-can-i-find-if-a-particular-package-exists-on-my-android-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!