2

Encontre en internet esta solucion ProxyType es para saber si es por PAC o DHCP, es un enum.

 /**
 * WIFI PROXY
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressWarnings("unchecked")
private static WifiConfiguration setProxy(WifiConfiguration conf, String hostname, int port, List<String> bypass, PROXY_TYPE type) {
    try {
        //linkProperties is no longer in WifiConfiguration
        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 = "STATIC";
                pi = ProxyInfo.buildDirectProxy(hostname, port, bypass);
                break;
            case PAC_URL:
                Type = "PAC";
                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;
}

Me funciona perfectamente pero el metodo buildDirectProxy dice que es para android 5 o superior. Hay alguna forma de hacerlo en 4.4+? Debe haber pues normalmente lo puedes hacer en estos sistemas en los ajustes. Gracias de antemano.

4
  • fijate si hay pistas aqui stackoverflow.com/q/12486441/1423096 el 13 jul. 2018 a las 1:02
  • Esa solución la intente pero si mal no recuerdo me daba el error de que estoy pidiendo un campo q no existe no recuerdo ahora se que lo pregunte en Internet y me dijeron q ese método el linkPropiertes lo habían cambiado
    – Alex Rivas
    el 13 jul. 2018 a las 1:56
  • y si implementas un método por cada versión? -> stackoverflow.com/a/3995388/1423096 el 13 jul. 2018 a las 2:07
  • pública tu respuesta y yo la aplico si me funciona la marco como aceptada
    – Alex Rivas
    el 13 jul. 2018 a las 2:15

1 respuesta 1

2

Este metodo lo resuelve. Muchas gracias a todos los que comentaron gracias a ellos pude llegar a esta respuesta ya esta probada y funciona bien. Muchas gracias a todos.

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

public static Object getDeclaredField(Object obj, String name)
        throws SecurityException, NoSuchFieldException,
        IllegalArgumentException, IllegalAccessException {
    Field f = obj.getClass().getDeclaredField(name);
    f.setAccessible(true);
    Object out = f.get(obj);
    return out;
}

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 void setProxySettings(String assign , WifiConfiguration wifiConf)
        throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException{
    setEnumField(wifiConf, assign, "proxySettings");
}


WifiConfiguration GetCurrentWifiConfiguration(WifiManager manager)
{
    if (!manager.isWifiEnabled())
        return null;

    List<WifiConfiguration> configurationList = manager.getConfiguredNetworks();
    WifiConfiguration configuration = null;
    int cur = manager.getConnectionInfo().getNetworkId();
    for (int i = 0; i < configurationList.size(); ++i)
    {
        WifiConfiguration wifiConfiguration = configurationList.get(i);
        if (wifiConfiguration.networkId == cur)
            configuration = wifiConfiguration;
    }

    return configuration;
}

private static WifiConfiguration setWifiProxySettings(WifiConfiguration config)
{
    if(null == config)
        return new WifiConfiguration();

    try
    {

        Object linkProperties = getField(config, "linkProperties");
        if(null == linkProperties)
            return new WifiConfiguration();;


        Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
        Class[] setHttpProxyParams = new Class[1];
        setHttpProxyParams[0] = proxyPropertiesClass;
        Class lpClass = Class.forName("android.net.LinkProperties");
        Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams);
        setHttpProxy.setAccessible(true);

        //get ProxyProperties constructor
        Class[] proxyPropertiesCtorParamTypes = new Class[3];
        proxyPropertiesCtorParamTypes[0] = String.class;
        proxyPropertiesCtorParamTypes[1] = int.class;
        proxyPropertiesCtorParamTypes[2] = String.class;

        Constructor proxyPropertiesCtor = proxyPropertiesClass.getConstructor(proxyPropertiesCtorParamTypes);


        Object[] proxyPropertiesCtorParams = new Object[3];
        proxyPropertiesCtorParams[0] = ""; // servidor proxy
        proxyPropertiesCtorParams[1] = 1; // puerto proxy
        proxyPropertiesCtorParams[2] = ""; // no proxy para


        Object proxySettings = proxyPropertiesCtor.newInstance(proxyPropertiesCtorParams);

        //pass the new object to setHttpProxy
        Object[] params = new Object[1];
        params[0] = proxySettings;
        setHttpProxy.invoke(linkProperties, params);

        setProxySettings("STATIC", config);


    }
    catch(Exception e)
    {
    }

    return config;
}
1
  • si alguien tiene un metodo mejor o mas claro y sencillo le agradeceria que me lo publicaran
    – Alex Rivas
    el 13 jul. 2018 a las 15:39

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.