下面是一些代码,可以让您设置/取消代理属性。它使用了上面链接中的一些代码。但是断开/重新连接后设置似乎不会生效。
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;
}
void setWifiProxySettings()
{
WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = GetCurrentWifiConfiguration(manager);
if(null == config)
return;
try
{
Object linkProperties = getField(config, "linkProperties");
if(null == linkProperties)
return;
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);
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] = "127.0.0.1";
proxyPropertiesCtorParams[1] = 8118;
proxyPropertiesCtorParams[2] = null;
Object proxySettings = proxyPropertiesCtor.newInstance(proxyPropertiesCtorParams);
Object[] params = new Object[1];
params[0] = proxySettings;
setHttpProxy.invoke(linkProperties, params);
setProxySettings("STATIC", config);
manager.updateNetwork(config);
manager.disconnect();
manager.reconnect();
}
catch(Exception e)
{
}
}
void unsetWifiProxySettings()
{
WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = GetCurrentWifiConfiguration(manager);
if(null == config)
return;
try
{
Object linkProperties = getField(config, "linkProperties");
if(null == linkProperties)
return;
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);
Object[] params = new Object[1];
params[0] = null;
setHttpProxy.invoke(linkProperties, params);
setProxySettings("NONE", config);
manager.updateNetwork(config);
manager.disconnect();
manager.reconnect();
}
catch(Exception e)
{
}
}