原文地址:https://blog.csdn.net/gaojinshan/article/details/51678968
扫描所有网络接口,查看是否有使用VPN的(接口名为tun0或ppp0):
- public static boolean isVpnUsed() {
- try {
- Enumeration<NetworkInterface> niList = NetworkInterface.getNetworkInterfaces();
- if(niList != null) {
- for (NetworkInterface intf : Collections.list(niList)) {
- if(!intf.isUp() || intf.getInterfaceAddresses().size() == 0) {
- continue;
- }
- Log.d(TAG, "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;
- }
比如,下面是所有网络接口的列表:
- c = {ArrayList@830054223664} size = 10
- 0 = {NetworkInterface@830057714392} "[p2p0][8][/fe80::7c7d:3dff:fe4d:ad0f%p2p0%8]"
- 1 = {NetworkInterface@830057715208} "[sit0][2]"
- 2 = {NetworkInterface@830054463232} "[tun0][30][/172.66.0.9]" ////这个接口就是VPN
- 3 = {NetworkInterface@830054320152} "[lo][1][/::1%1%1][/127.0.0.1]"
- 4 = {NetworkInterface@830054286792} "[wlan0][9]"
- 5 = {NetworkInterface@830054270304} "[rmnet0][3][/100.84.44.157]"
- 6 = {NetworkInterface@830054259216} "[rmnet1][4]"
- 7 = {NetworkInterface@830054246600} "[rmnet3][6]"
- 8 = {NetworkInterface@830054236272} "[rmnet2][5]"
- 9 = {NetworkInterface@830054224840} "[rmnet4][7]"