问题描述
我如何设置使用Java ProxySettings和ProxyProperties在Android Wi-Fi连接(编程)?
由于ipAssignment,linkProperties,ProxySettings和ProxyProperties都隐藏在WifiConfiguration领域在Android 3.1及以上,我需要能够枚举类和使用的字段。
随着使用下面的链接,我可以设置一个静态IP地址,网关和DNS为特定的Wi-Fi连接,但我还需要设置Wificonfiguration.ProxySettings.STATIC和ProxyProperties
请参阅堆栈溢出问题的 如何编程configue一个静态IP地址,子网掩码,网关在Android 3.x或4.x版 的。
例如,
WifiConfiguration配置=新WifiConfiguration(配置);
config.ipAssignment = WifiConfiguration.IpAssignment.UNASSIGNED;
config.proxySettings = WifiConfiguration.ProxySettings.STATIC;
config.linkProperties.setHttpProxy(新ProxyProperties(127.0.0.1,3128,));
寻找这样的:
setProxySettings(静态,wifiConf);
setProxyProperties(proxyserver.mine.com.au,8080,); //设置代理服务器和端口。
wifiManager.updateNetwork(wifiConf); //应用设置
从coolypf .ipAssignment .ProxySettings使用下面的code和linkProperties被隐藏...
WifiManager经理=(WifiManager)getSystemService(Context.WIFI_SERVICE);
manager.asyncConnect(这一点,新处理程序());
如果回报(manager.isWifiEnabled()!);
名单< WifiConfiguration> configurationList = manager.getConfiguredNetworks();
WifiConfiguration配置= NULL;
INT CUR = manager.getConnectionInfo()getNetworkId()。
的for(int i = 0; I< configurationList.size(); ++ I)
{
WifiConfiguration wifiConfiguration = configurationList.get(ⅰ);
如果(wifiConfiguration.networkId == CUR)
配置= wifiConfiguration;
}
如果(配置== NULL)回报;
WifiConfiguration配置=新WifiConfiguration(配置);
config.ipAssignment = WifiConfiguration.IpAssignment.UNASSIGNED;
config.proxySettings = WifiConfiguration.ProxySettings.STATIC;
config.linkProperties.clear();
config.linkProperties.setHttpProxy(新ProxyProperties(127.0.0.1,3128,));
manager.saveNetwork(配置);
下面是一些code,它应该让你设置/取消ProxyProperties。它使用了一些从上面的链接相同的code。这些设置似乎并没有采取与断开连接/重新连接效果。
公共静态对象getfield命令(obj对象,字符串名称)
抛出SecurityException异常,NoSuchFieldException,抛出:IllegalArgumentException,IllegalAccessException {
域F = obj.getClass()getfield命令(名称)。
对象OUT = f.get(OBJ);
返回了;
}
公共静态对象getDeclaredField(obj对象,字符串名称)
抛出SecurityException异常,NoSuchFieldException,
抛出:IllegalArgumentException,IllegalAccessException {
域F = obj.getClass()getDeclaredField(名称)。
f.setAccessible(真正的);
对象OUT = f.get(OBJ);
返回了;
}
公共静态无效setEnumField(obj对象,字符串值,字符串名称)
抛出SecurityException异常,NoSuchFieldException,抛出:IllegalArgumentException,IllegalAccessException {
域F = obj.getClass()getfield命令(名称)。
f.set(OBJ,Enum.valueOf((类<枚举>)f.getType(),值));
}
公共静态无效setProxySettings(字符串赋值,WifiConfiguration wifiConf)
抛出SecurityException异常,抛出:IllegalArgumentException,NoSuchFieldException,IllegalAccessException {
setEnumField(wifiConf,分配,proxySettings);
}
WifiConfiguration GetCurrentWifiConfiguration(WifiManager经理)
{
如果(!manager.isWifiEnabled())
返回null;
名单< WifiConfiguration> configurationList = manager.getConfiguredNetworks();
WifiConfiguration配置= NULL;
INT CUR = manager.getConnectionInfo()getNetworkId()。
的for(int i = 0; I< configurationList.size(); ++ I)
{
WifiConfiguration wifiConfiguration = configurationList.get(ⅰ);
如果(wifiConfiguration.networkId == CUR)
配置= wifiConfiguration;
}
返回的配置;
}
无效setWifiProxySettings()
{
//获取当前的WiFi配置
WifiManager经理=(WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiConfiguration配置= GetCurrentWifiConfiguration(经理);
如果(空==配置)
返回;
尝试
{
//从无线网络配置中的链接属性
对象linkProperties = getfield命令(配置linkProperties);
如果(空== linkProperties)
返回;
//获取LinkProperties的setHttpProxy方法
类proxyPropertiesClass =的Class.forName(android.net.ProxyProperties);
类[] setHttpProxyParams =新类[1];
setHttpProxyParams [0] = proxyPropertiesClass;
类lpClass =的Class.forName(android.net.LinkProperties);
方法setHttpProxy = lpClass.getDeclaredMethod(setHttpProxy,setHttpProxyParams);
setHttpProxy.setAccessible(真正的);
//获取ProxyProperties构造
类[] proxyPropertiesCtorParamTypes =新等级[3];
proxyPropertiesCtorParamTypes [0] =为String.class;
proxyPropertiesCtorParamTypes [1] = int.class;
proxyPropertiesCtorParamTypes [2] =为String.class;
构造proxyPropertiesCtor = proxyPropertiesClass.getConstructor(proxyPropertiesCtorParamTypes);
//创建参数的构造函数
[对象] proxyPropertiesCtorParams =新的对象[3]。
proxyPropertiesCtorParams [0] =127.0.0.1;
proxyPropertiesCtorParams [1] = 8118;
proxyPropertiesCtorParams [2] =无效;
//使用PARAMS创建新对象
对象proxySettings = proxyPropertiesCtor.newInstance(proxyPropertiesCtorParams);
//通过新的对象来setHttpProxy
[对象] PARAMS =新的对象[1];
PARAMS [0] = proxySettings;
setHttpProxy.invoke(linkProperties,则params);
setProxySettings(静态,配置);
//保存设置
manager.updateNetwork(配置);
manager.disconnect();
manager.reconnect();
}
赶上(例外五)
{
}
}
无效unsetWifiProxySettings()
{
WifiManager经理=(WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiConfiguration配置= GetCurrentWifiConfiguration(经理);
如果(空==配置)
返回;
尝试
{
//从无线网络配置中的链接属性
对象linkProperties = getfield命令(配置linkProperties);
如果(空== linkProperties)
返回;
//获取LinkProperties的setHttpProxy方法
类proxyPropertiesClass =的Class.forName(android.net.ProxyProperties);
类[] setHttpProxyParams =新类[1];
setHttpProxyParams [0] = proxyPropertiesClass;
类lpClass =的Class.forName(android.net.LinkProperties);
方法setHttpProxy = lpClass.getDeclaredMethod(setHttpProxy,setHttpProxyParams);
setHttpProxy.setAccessible(真正的);
//传递null作为代理
[对象] PARAMS =新的对象[1];
PARAMS [0] =无效;
setHttpProxy.invoke(linkProperties,则params);
setProxySettings(NONE,配置);
//保存配置
manager.updateNetwork(配置);
manager.disconnect();
manager.reconnect();
}
赶上(例外五)
{
}
}
How can I set ProxySettings and ProxyProperties on an Android Wi-Fi connection using Java (programatically)?
As ipAssignment, linkProperties, ProxySettings and ProxyProperties are hidden fields within WifiConfiguration on Android 3.1 and up, I need to be able to enum the class and use the fields.
Following the code sample using the link below, I can set a static IP address, gateway and DNS for a particular Wi-Fi connection, but I also need to set Wificonfiguration.ProxySettings.STATIC and ProxyProperties
See Stack Overflow question How to configue a static IP address, netmask, gateway programmatically on Android 3.x or 4.x.
For example,
WifiConfiguration config = new WifiConfiguration(configuration);
config.ipAssignment = WifiConfiguration.IpAssignment.UNASSIGNED;
config.proxySettings = WifiConfiguration.ProxySettings.STATIC;
config.linkProperties.setHttpProxy(new ProxyProperties("127.0.0.1", 3128, ""));
Looking for something like:
setProxySettings("STATIC", wifiConf);
setProxyProperties("proxyserver.mine.com.au", 8080, ""); // Set Proxy server and port.
wifiManager.updateNetwork(wifiConf); //apply the setting
Using the following code from coolypf .ipAssignment .ProxySettings and linkProperties are hidden...
WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
manager.asyncConnect(this, new Handler());
if (!manager.isWifiEnabled()) return;
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;
}
if (configuration == null) return;
WifiConfiguration config = new WifiConfiguration(configuration);
config.ipAssignment = WifiConfiguration.IpAssignment.UNASSIGNED;
config.proxySettings = WifiConfiguration.ProxySettings.STATIC;
config.linkProperties.clear();
config.linkProperties.setHttpProxy(new ProxyProperties("127.0.0.1", 3128, ""));
manager.saveNetwork(config);
Here's some code that should allow you to set/unset ProxyProperties. It uses some of the same code from the link above. The settings do not seem to take effect with the disconnect/reconnect.
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()
{
//get the current wifi configuration
WifiManager manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = GetCurrentWifiConfiguration(manager);
if(null == config)
return;
try
{
//get the link properties from the wifi configuration
Object linkProperties = getField(config, "linkProperties");
if(null == linkProperties)
return;
//get the setHttpProxy method for LinkProperties
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);
//create the parameters for the constructor
Object[] proxyPropertiesCtorParams = new Object[3];
proxyPropertiesCtorParams[0] = "127.0.0.1";
proxyPropertiesCtorParams[1] = 8118;
proxyPropertiesCtorParams[2] = null;
//create a new object using the params
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);
//save the settings
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
{
//get the link properties from the wifi configuration
Object linkProperties = getField(config, "linkProperties");
if(null == linkProperties)
return;
//get the setHttpProxy method for LinkProperties
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);
//pass null as the proxy
Object[] params = new Object[1];
params[0] = null;
setHttpProxy.invoke(linkProperties, params);
setProxySettings("NONE", config);
//save the config
manager.updateNetwork(config);
manager.disconnect();
manager.reconnect();
}
catch(Exception e)
{
}
}
这篇关于如何设置使用Java ProxySettings和ProxyProperties在Android Wi-Fi连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!