解决方案
有下面两种方法:
fun isAppInstalled(context: Context, packageName: String): Boolean {
val packageManager = context.packageManager
var isInstalled = false
try {
val pInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_GIDS)
isInstalled = pInfo != null
} catch (e: Exception) {
}
return isInstalled
}
fun isAppInstalled2(context: Context, packageName: String): Boolean {
val packageManager = context.packageManager
// 获取所有已安装程序的包信息
val pInfo = packageManager.getInstalledPackages(0)
for (i in pInfo.indices) {
// 循环判断是否存在指定包名
if (pInfo[i].packageName.equals(packageName, ignoreCase = true)) {
return true
}
}
return false
}
网友评论