Detect behind VPN in android

前端 未结 5 903
半阙折子戏
半阙折子戏 2021-01-05 22:49

How to detect programatically if traffic is going through VPN without using intent to connect to VPNService. Is there some system call?

5条回答
  •  一生所求
    2021-01-05 23:01

    I guess for most use-cases you want to check if the actively used network is using VPN instead of if just any network available is using VPN.

    connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_VPN)
    

    ... is returning a NetworkInfo for an available VPN Network. This might not be the active Network! Instead you could use the following snippet to test if the active Network is using VPN:

    Network activeNetwork = connectivityManager.getActiveNetwork();
    NetworkCapabilities caps = connectivityManager.getNetworkCapabilities(activeNetwork);
    boolean vpnInUse = caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
    

提交回复
热议问题