Android VpnService - How to check VpnService if it was started?

丶灬走出姿态 提交于 2019-12-07 13:24:12

问题


I have two applications that uses VpnService class. But there can be only one VPN connection running at the same time. The existing interface is deactivated when a new one is created and I want the new application not to start vpnservice to avoid old one getting deactivated and its VPN connection. So I want to check if another vpnservice was started before invoke startService().

Is there any api to do this ?


回答1:


As far as I know you can not check directly.The below is the approach that I use in my application for checking VPN connection.

1)Make a HTTPGet call for http://jsonip.com

                    public static String GetDeviceIp(){

                    HttpGet req = new HttpGet("http://jsonip.com");

                    DefaultHttpClient httpClient = new DefaultHttpClient();

                    HttpResponse response = httpClient.execute(req); 


                    if (response.getStatusLine().getStatusCode() == 200){
                        ipaddress=parseJson(response.getEntity().getContent());

                    }else
                    { ipaddress ="EMPTY" }

                    return ipaddress
          }

2)Parse the json response and extract the Ip address from response

      public static String parseJson(InputStream in)
      {
         String iP;
        try{

        String result;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = bufferedReader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString(); 

        JSONObject jsonObject = new JSONObject(result);
        iP=jsonObject.getString("ip");
    }catch (Exception e){
        iP="EMPTY";
    }

    return iP;
   }

3)Compare if VPN server Ip and extracted Ip are equal if so then the VPN is on else VPN is off

public static boolean IsVPNON(){
    return GetDeviceIp().equals("VPN-IP address");}



回答2:


While above approach works, it is more or less a hack and requires sending an HTTP request.

Since this seems like a one-off check you want to run before starting another VPN, you can check whether VPN network is part of all available networks.

private boolean isAnyVpnConnected(Context context) {
    ConnectivityManager connectivityManager =
            (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
    Network[] networks = connectivityManager.getAllNetworks();
    if (networks == null) {
        return false;
    }
    for (Network network : networks) {
        NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
        if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN) &&
                !capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) {
            return true;
        }
    }
    return false;
}


来源:https://stackoverflow.com/questions/22057502/android-vpnservice-how-to-check-vpnservice-if-it-was-started

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!