安卓开发

安卓检测是否有安装某个app(应用)

2020-08-12  本文已影响0人  蓝不蓝编程

解决方案

有下面两种方法:

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
}
上一篇下一篇

猜你喜欢

热点阅读