How can I find whether a particular package or application, say: com.android.abc
, exists on my Android device?
如何在我的Android设备上找到特定的包或应用程序,例如:com.android.abc?
6 个解决方案
#1
125
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
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;
#3
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.
您应该使用PackageManager的函数getInstalledPackages()来获取所有已安装软件包的列表以及搜索您感兴趣的软件包。请注意软件包名称位于PackageInfo.packageName字段中。
#4
3
Without using a try-catch block or iterating through a bunch of packages:
不使用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;
}
#5
1
If you just want to use adb:
如果您只想使用adb:
adb shell "pm list packages"|cut -f 2 -d ":"
it will list all installed packages.
它将列出所有已安装的软件包。
#6
0
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.
由于某些设备已报告“getInstalledPackages”可能导致TransactionTooLargeException(请在这里,此处和此处查看),我认为您应该像我在下面所做的那样进行回退。
This issue was supposed to be fixed on Android 5.1 (read here), but some still reported about it.
这个问题应该在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
125
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
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;
#3
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.
您应该使用PackageManager的函数getInstalledPackages()来获取所有已安装软件包的列表以及搜索您感兴趣的软件包。请注意软件包名称位于PackageInfo.packageName字段中。
#4
3
Without using a try-catch block or iterating through a bunch of packages:
不使用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;
}
#5
1
If you just want to use adb:
如果您只想使用adb:
adb shell "pm list packages"|cut -f 2 -d ":"
it will list all installed packages.
它将列出所有已安装的软件包。
#6
0
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.
由于某些设备已报告“getInstalledPackages”可能导致TransactionTooLargeException(请在这里,此处和此处查看),我认为您应该像我在下面所做的那样进行回退。
This issue was supposed to be fixed on Android 5.1 (read here), but some still reported about it.
这个问题应该在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) {
}
}