How to set the proxy of a wifi in android 4.x?

0

This is the code. Type is an enum that contains two DHCP and PAC values. The problem is that this code tells me that the call requires android 21 and I need this to work on 18. Is there any way? Thanks in advance.

private static WifiConfiguration setProxy(WifiConfiguration conf, String hostname, int port, List<String> bypass, PROXY_TYPE type) {
try {
    Class proxyInfoClass = Class.forName("android.net.ProxyInfo");
    Class[] setHttpProxyParams = new Class[1];
    setHttpProxyParams[0] = proxyInfoClass;
    Class wifiConfigClass = Class.forName("android.net.wifi.WifiConfiguration");
    Method setHttpProxy = wifiConfigClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams);
    setHttpProxy.setAccessible(true);

    //Get the ENUM ProxySettings in IpConfiguration
    Class ipConfigClass = Class.forName("android.net.IpConfiguration");
    Field f = ipConfigClass.getField("proxySettings");
    Class proxySettingsClass = f.getType();

    Class[] setProxySettingsParams = new Class[1];
    setProxySettingsParams[0] = proxySettingsClass;
    Method setProxySettings = wifiConfigClass.getDeclaredMethod("setProxySettings", setProxySettingsParams);
    setProxySettings.setAccessible(true);


    ProxyInfo pi = null;
    String Type = null;
    switch (type) {
        case MANUAL:
            Type = "DHCP";
            //esta es la llamada que me dice API21
            pi = ProxyInfo.buildDirectProxy(hostname, port, bypass);
            break;
        case PAC_URL:
            Type = "PAC";
            //esta tambien
            pi = ProxyInfo.buildPacProxy(Uri.parse(hostname));
    }

    //pass the new object to setHttpProxy
    Object[] params_SetHttpProxy = new Object[1];
    params_SetHttpProxy[0] = pi;
    setHttpProxy.invoke(conf, params_SetHttpProxy);

    //pass the enum to setProxySettings
    Object[] params_setProxySettings = new Object[1];
    params_setProxySettings[0] = Enum.valueOf((Class<Enum>) proxySettingsClass, Type);
    setProxySettings.invoke(conf, params_setProxySettings);
} catch (Exception e) {
    e.printStackTrace();
}
return conf;

}

    
asked by Alex Rivas 23.05.2018 в 18:53
source

0 answers