A. Android 是怎麼判斷某個APK文件是否已經安裝
Android系統中,判斷應用有無安裝有兩種方式:
1.根據包名判斷,以下為判斷代碼:
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;
}