android WIFI 設定代理程式碼 4.4.3——5.0

世界美景發表於2017-11-03

記錄下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;
            }
        }
    }

相關文章