android中设置代理代码

发布者:世界美景
发布于:2017-11-03 14:30

记录下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;
    }

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;
            }
        }
    }

声明:该文观点仅代表作者本人,转载请注明来自看雪