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 ?
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");}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments