android WIFI 设置代理代码 4.4.3——5.0

将相本无种,男儿当自强。这篇文章主要讲述android WIFI 设置代理代码 4.4.3——5.0相关的知识,希望能为你提供帮助。
【android WIFI 设置代理代码 4.4.3——5.0】记录下android中设置代理代码 或许有朋友能用的上
适用于4.4.3 在5.0上android.net.ProxyProperties 找不到 估计API被谷歌拿掉了 4.4.4还没试估计API还在

private static String NOTPROXY = ""; //不走代理名单privatestaticList< String> list; //type为1设置wifi设置为0是清除代理 public static void setWifi(Context context, final String ip, final int port, final int type) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, ClassNotFoundException, InstantiationException, InvocationTargetException { final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); final String ssid = wifiManager.getConnectionInfo().getSSID(); List< WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks(); changeNotProxy(); for (WifiConfiguration config : configuredNetworks) { Log.d("txy", "current SSID is" + config.SSID); if (config.SSID.toString().contains(Common.MYSSID)) {//需要给哪个无线设置代理 Class proxyPropertiesClass = Class .forName("android.net.ProxyProperties"); Constructor c = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE, String.class); c.setAccessible(true); //wifiIP 为代理的IP地址如:192.168.0.111wifiPort为代理端口 如888setProxy(config, (type == 0 ? null : c.newInstance(ip, port, NOTPROXY)), type); wifiManager.updateNetwork(config); break; } else { //wifiManager.removeNetwork(config.networkId); } } wifiManager.disconnect(); wifiManager.reconnect(); } public static void setObjField(Object obj, Object value, String name) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field f = obj.getClass().getDeclaredField(name); f.setAccessible(true); f.set(obj, value); }//type为1设置wifi设置为0是清除代理 public static WifiConfiguration setProxy(WifiConfiguration wifiConf, Object properties, int type) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, ClassNotFoundException, InstantiationException, InvocationTargetException { if (type == 0) setEnumField(wifiConf, "NONE", "proxySettings"); else setEnumField(wifiConf, "STATIC", "proxySettings"); Object linkProperties = getField(wifiConf, "linkProperties"); setObjField(linkProperties, properties, "mHttpProxy"); setObjField(wifiConf, linkProperties, "linkProperties"); return wifiConf; }public static void setEnumField(Object obj, String value, String name) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field f = obj.getClass().getField(name); f.set(obj, Enum.valueOf((Class< Enum> ) f.getType(), value)); }public static Object getField(Object obj, String name) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field f = obj.getClass().getField(name); Object out = f.get(obj); return out; }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
5.0
privatestaticList< String> list; //不走代理名单 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public static void set_proxy(Application context, String ip, String port, int type) throws Throwable { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wi = wifiManager.getConnectionInfo(); List< WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks(); changeNotProxy(); for(WifiConfiguration config : configuredNetworks) { //需要给哪个无线设置代理 if(config.SSID.equals(wi.getSSID())) {Class proxySettings = Class.forName("android.net.IpConfiguration$ProxySettings"); Class[] setProxyParams = new Class[2]; setProxyParams[0] = proxySettings; setProxyParams[1] = ProxyInfo.class; Method setProxy = config.getClass().getDeclaredMethod("setProxy", setProxyParams); setProxy.setAccessible(true); if(type == 0) { Object[] methodParams = new Object[2]; methodParams[0] = Enum.valueOf(proxySettings, "NONE"); methodParams[1] = null; setProxy.invoke(config, methodParams); } else { ProxyInfo desiredProxy = ProxyInfo.buildDirectProxy(ip, Integer.parseInt(port),list); Object[] methodParams = new Object[2]; methodParams[0] = Enum.valueOf(proxySettings, "STATIC"); methodParams[1] = desiredProxy; setProxy.invoke(config, methodParams); }//save the settings wifiManager.updateNetwork(config); wifiManager.disconnect(); wifiManager.reconnect(); break; } } }


    推荐阅读