Android 是怎么判断某个APK文件是不是已经安装
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 是怎么判断某个APK文件是不是已经安装相关的知识,希望对你有一定的参考价值。
目前android有两种主流的判断方式,第一种是根据程序包的名称(packageName)来判断。另外一种就是根据intent来判断,具体代码如下:
根据包名判断,以下为判断代码:
public boolean checkApkExist(Context context, String packageName)
if (packageName == null || “”.equals(packageName))
return false;
try
ApplicationInfo info = context.getPackageManager()
.getApplicationInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
catch (NameNotFoundException e)
return false;
2. 根据Intent判断,以下为判断代码:
public boolean checkApkExist(Context context, Intent intent)
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, 0);
if(list.size() > 0)
return true;
return false;
public boolean checkApkExist(Context context, String packageName)
if (packageName == null || “”.equals(packageName)) return false;
try
ApplicationInfo info = context.getPackageManager() .getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES); return true;
catch (NameNotFoundException e) return false;
public boolean checkApkExist(Context context, Intent intent)
List<ResolveInfo> list = context.getPackageManager()
.queryIntentActivities(intent, 0);
if (list.size() > 0)
return true;
return false;
以上代码只是基于安卓系统来判断,对于ios系统无效。
privateboolean isAppInstalled(String uri)
PackageManager pm = getPackageManager();
boolean installed =false;
try
pm.getPackageInfo(uri,PackageManager.GET_ACTIVITIES);
installed =true;
catch(PackageManager.NameNotFoundException e)
installed =false;
return installed;
if(isAppInstalled("com.yourpackage.package"))
//app installed
else
//app not installed
参考技术B PackageManager本回答被提问者和网友采纳
以上是关于Android 是怎么判断某个APK文件是不是已经安装的主要内容,如果未能解决你的问题,请参考以下文章