码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-09-15 20:13:58      阅读:623      评论:0      收藏:0      [点我收藏+]

标签:ipo   conf   pop   span   declare   lol   param   illegal   out   

记录下android中设置代理代码 或许有朋友能用的上

适用于4.4.3 在5.0上android.net.ProxyProperties 找不到 估计API被谷歌拿掉了 4.4.4还没试估计API还在

private static String NOTPROXY = ""; //不走代理名单

     private  static   List<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.111   wifiPort为代理端口 如888

                setProxy(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

  private  static   List<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;
            }
        }
    }

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

标签:ipo   conf   pop   span   declare   lol   param   illegal   out   

原文地址:http://www.cnblogs.com/gongxiaojiu/p/7527717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!