问题描述
我有一个使用VpnService类两个应用程序。但是,可以在同一时间只运行一个VPN连接。创建一个新的时和现有的接口被禁用我想新的应用程序不启动vpnservice避免旧得到停用,它的VPN连接。所以,我要检查,如果另一个vpnservice在开始之前调用startService()。
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().
有没有API来做到这一点?
Is there any api to do this ?
推荐答案
据我知道你不能检查directly.The下面是我在我的应用程序用于检查VPN连接的方法。
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)做一个HTTPGET呼吁
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)解析JSON响应,从响应中提取IP地址
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)如果VPN服务器的IP并提取ip相等比较,如果这样那么VPN是其他VPN是关闭
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");}
这篇关于Android的VpnService - 如何检查VpnService是否已启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!