美文网首页安卓开发
安卓检测是否有安装某个app(应用)

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

作者: 蓝不蓝编程 | 来源:发表于2020-08-12 20:27 被阅读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
    }
    

    相关文章

      网友评论

        本文标题:安卓检测是否有安装某个app(应用)

        本文链接:https://www.haomeiwen.com/subject/cddydktx.html