如何查找我的Android设备上是否存在特定的软件包?


Answers:


152

使用程序包名称调用以下任何方法。

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

感谢Rasel,我只是在寻找这个。十分感谢。
布里格

我必须导入什么才能使其正常工作?我cannot resolve symbol 'PackageManager'出错了。
奥斯卡·斯旺罗斯

第二种方法更快
Alex

13

无需使用try-catch块或遍历一堆程序包:

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

7

科特林

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

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;


2

如果您只想使用adb:

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

它将列出所有已安装的软件包。


2

您可以使用pm.getPackageUid()而不是遍历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;

getPackageUid-但是在API级别24中添加。
Goran Horia Mihail

1

由于某些设备已经报告“ getInstalledPackages”会导致TransactionTooLargeException(请在此处此处此处检查),因此我认为您也应该像下面那样进行回退。

这个问题原本应该在Android 5.1上解决(请在此处阅读),但仍有一些报道。

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

1

我们可以这样检查:

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

制作包装检查方法!此信用归于“ Kavi” https://stackoverflow.com/a/30708227/6209105

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;

}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.