Detect behind VPN in android

前端 未结 5 954
半阙折子戏
半阙折子戏 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 22:57

    It is possible to check it in Android API 21 an higher, but it seems doesn't work (on nexus 5 @ 5.0.1, nexus 7 @ 5.0.2). Why its not working?

    Snippet of new API for check it (and all traffic is routed by vpn if connected):

    @Inject
    boolean checkVPN(ConnectivityManager connMgr) {
        //don't know why always returns null:
        NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_VPN);
    
        boolean isVpnConn = networkInfo == null ? false : networkInfo.isConnected();
        return isVpnConn;
    }
    

提交回复
热议问题