如何查找com.android.abc
Android设备上是否存在特定的程序包或应用程序,例如:?
Answers:
使用程序包名称调用以下任何方法。
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;
}
cannot resolve symbol 'PackageManager'
出错了。
无需使用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;
}
您应该使用PackageManager
调用的函数getInstalledPackages()
来获取所有已安装软件包的列表,并搜索您感兴趣的软件包。请注意,软件包名称位于PackageInfo.packageName
字段中。
您可以使用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;
由于某些设备已经报告“ 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) {
}
}
我们可以这样检查:
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;
}