Android应用判断是否使用VPN以及是否使用代理

  1. /**
  2. * 是否使用代理(WiFi状态下的,避免被抓包)
  3. */
  4. private boolean isWifiProxy(){
  5. final boolean is_ics_or_later = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
  6. String proxyAddress;
  7. int proxyPort;
  8. if (is_ics_or_later) {
  9. proxyAddress = System.getProperty( "http.proxyHost");
  10. String portstr = System.getProperty( "http.proxyPort");
  11. proxyPort = Integer.parseInt((portstr != null ? portstr : "-1"));
  12. System.out.println(proxyAddress + "~");
  13. System.out.println( "port = " + proxyPort);
  14. } else {
  15. proxyAddress = android.net.Proxy.getHost(MainActivity. this);
  16. proxyPort = android.net.Proxy.getPort(MainActivity. this);
  17. Log.e( "address = ", proxyAddress + "~");
  18. Log.e( "port = ", proxyPort + "~");
  19. }
  20. return (!TextUtils.isEmpty(proxyAddress)) && (proxyPort != - 1);
  21. }
  22. /**
  23. * 是否正在使用VPN
  24. */
  25. public static boolean isVpnUsed() {
  26. try {
  27. Enumeration niList = NetworkInterface.getNetworkInterfaces();
  28. if(niList != null) {
  29. for (NetworkInterface intf : Collections.list(niList)) {
  30. if(!intf.isUp() || intf.getInterfaceAddresses().size() == 0) {
  31. continue;
  32. }
  33. Log.d( "-----", "isVpnUsed() NetworkInterface Name: " + intf.getName());
  34. if ( "tun0".equals(intf.getName()) || "ppp0".equals(intf.getName())){
  35. return true; // The VPN is up
  36. }
  37. }
  38. }
  39. } catch (Throwable e) {
  40. e.printStackTrace();
  41. }
  42. return false;
  43. }
  1. /**
  2. * 是否使用代理(WiFi状态下的,避免被抓包)
  3. */
  4. private boolean isWifiProxy(){
  5. final boolean is_ics_or_later = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
  6. String proxyAddress;
  7. int proxyPort;
  8. if (is_ics_or_later) {
  9. proxyAddress = System.getProperty( "http.proxyHost");
  10. String portstr = System.getProperty( "http.proxyPort");
  11. proxyPort = Integer.parseInt((portstr != null ? portstr : "-1"));
  12. System.out.println(proxyAddress + "~");
  13. System.out.println( "port = " + proxyPort);
  14. } else {
  15. proxyAddress = android.net.Proxy.getHost(MainActivity. this);
  16. proxyPort = android.net.Proxy.getPort(MainActivity. this);
  17. Log.e( "address = ", proxyAddress + "~");
  18. Log.e( "port = ", proxyPort + "~");
  19. }
  20. return (!TextUtils.isEmpty(proxyAddress)) && (proxyPort != - 1);
  21. }
  22. /**
  23. * 是否正在使用VPN
  24. */
  25. public static boolean isVpnUsed() {
  26. try {
  27. Enumeration niList = NetworkInterface.getNetworkInterfaces();
  28. if(niList != null) {
  29. for (NetworkInterface intf : Collections.list(niList)) {
  30. if(!intf.isUp() || intf.getInterfaceAddresses().size() == 0) {
  31. continue;
  32. }
  33. Log.d( "-----", "isVpnUsed() NetworkInterface Name: " + intf.getName());
  34. if ( "tun0".equals(intf.getName()) || "ppp0".equals(intf.getName())){
  35. return true; // The VPN is up
  36. }
  37. }
  38. }
  39. } catch (Throwable e) {
  40. e.printStackTrace();
  41. }
  42. return false;
  43. }

猜你喜欢

转载自blog.csdn.net/qq_15949077/article/details/80870870