vpn检测

作者: CentForever | 来源:发表于2021-05-07 15:50 被阅读0次

    检测手机是否打开vpn

    import android.content.Context
    import android.net.ConnectivityManager
    import android.net.NetworkCapabilities
    import android.os.Build
    
    fun Context.isUsingVpn(): Boolean? {
        var vpnInUse = false
        val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val activeNetwork = connectivityManager?.activeNetwork
            val caps = connectivityManager?.getNetworkCapabilities(activeNetwork)
            return caps?.hasTransport(NetworkCapabilities.TRANSPORT_VPN)
        }
        val networks = connectivityManager?.allNetworks
        networks?.let {
            for (i in networks.indices) {
                val caps = connectivityManager.getNetworkCapabilities(networks[i])
                if (caps?.hasTransport(NetworkCapabilities.TRANSPORT_VPN) == true) {
                    vpnInUse = true
                    break
                }
            }
        }
        return vpnInUse
    }
    

    相关文章

      网友评论

          本文标题:vpn检测

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