1.新建一个vpn.mjava文件,代码如下:
/**
* 检测是否使用代理(WiFi状态下的,避免被抓包),如果在使用返回true,反之返回flase
*/
private boolean isWifiProxy(){
final boolean is_ics_or_later = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
String proxyAddress;
int proxyPort;
if (is_ics_or_later) {
proxyAddress = System.getProperty("http.proxyHost");
String portstr = System.getProperty("http.proxyPort");
proxyPort = Integer.parseInt((portstr != null ? portstr : "-1"));
System.out.println(proxyAddress + "~");
System.out.println("port = " + proxyPort);
}else {
proxyAddress = android.net.Proxy.getHost(MainActivity.this);
proxyPort = android.net.Proxy.getPort(MainActivity.this);
Log.e("address = ", proxyAddress + "~");
Log.e("port = ", proxyPort + "~");
}
return (!TextUtils.isEmpty(proxyAddress)) && (proxyPort != -1);
}
/**
* 检测是否正在使用VPN,如果在使用返回true,反之返回flase
*/
public static boolean isVpnUsed() {
try {
Enumeration niList = NetworkInterface.getNetworkInterfaces();
if(niList != null) {
for (NetworkInterface intf : Collections.list(niList)) {
if(!intf.isUp() || intf.getInterfaceAddresses().size() == 0) {
continue;
}
Log.d("-----", "isVpnUsed() NetworkInterface Name: " + intf.getName());
if ("tun0".equals(intf.getName()) || "ppp0".equals(intf.getName())){
return true; // The VPN is up
}
}
}
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}
网友评论