由于android5.0版本,对这个WifiConfiguration这个类做了一些改动,所以设置起来比以前的版本有些改动!
如若想要使用5.0以前的方法的话这个比较不错,4.x的版本都能用。。。
源码里WifiConfiguration这个类里有专门提供设置的方法,但是都被hide掉了,所以可以利用反射调用,感觉繁琐的同学可以使用源码编译时生成的全编译的jar包classes.jar!
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) f.getType(), value));
}
public static Object getDeclaredFieldObject(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 setDeclardFildObject(Object obj,String name,Object object){
Field f = null;
try {
f = obj.getClass().getDeclaredField(name);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
f.setAccessible(true);
try {
f.set(obj,object);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
// 获取当前的Wifi连接
public static WifiConfiguration getCurrentWifiConfiguration(WifiManager wifiManager) {
if (!wifiManager.isWifiEnabled())
return null;
List configurationList = wifiManager.getConfiguredNetworks();
WifiConfiguration configuration = null;
int cur = wifiManager.getConnectionInfo().getNetworkId();
// Log.d("当前wifi连接信息",wifiManager.getConnectionInfo().toString());
for (int i = 0; i < configurationList.size(); ++i) {
WifiConfiguration wifiConfiguration = configurationList.get(i);
if (wifiConfiguration.networkId == cur)
configuration = wifiConfiguration;
}
return configuration;
}
这段代码主要是提取一些反射的方法
/**
* 设置代理信息 exclList是添加不用代理的网址用的
* */
public void setHttpPorxySetting(Context context,String host, int port, List exclList)
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
IllegalAccessException, NoSuchFieldException {
WifiManager wifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = getCurrentWifiConfiguration(wifiManager);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
mInfo = ProxyInfo.buildDirectProxy(host,port);
}
if (config != null){
Class clazz = Class.forName("android.net.wifi.WifiConfiguration");
Class parmars = Class.forName("android.net.ProxyInfo");
Method method = clazz.getMethod("setHttpProxy",parmars);
method.invoke(config,mInfo);
Object mIpConfiguration = getDeclaredFieldObject(config,"mIpConfiguration");
setEnumField(mIpConfiguration, "STATIC", "proxySettings");
setDeclardFildObject(config,"mIpConfiguration",mIpConfiguration);
//save the settings
wifiManager.updateNetwork(config);
wifiManager.disconnect();
wifiManager.reconnect();
}
}
/**
* 取消代理设置
* */
public void unSetHttpProxy(Context context)
throws ClassNotFoundException, InvocationTargetException, IllegalAccessException,
NoSuchFieldException, NoSuchMethodException {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration configuration = getCurrentWifiConfiguration(wifiManager);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
mInfo = ProxyInfo.buildDirectProxy(null,0);
}
if (configuration != null){
Class clazz = Class.forName("android.net.wifi.WifiConfiguration");
Class parmars = Class.forName("android.net.ProxyInfo");
Method method = clazz.getMethod("setHttpProxy",parmars);
method.invoke(configuration,mInfo);
Object mIpConfiguration = getDeclaredFieldObject(configuration,"mIpConfiguration");
setEnumField(mIpConfiguration, "NONE", "proxySettings");
setDeclardFildObject(configuration,"mIpConfiguration",mIpConfiguration);
//保存设置
wifiManager.updateNetwork(configuration);
wifiManager.disconnect();
wifiManager.reconnect();
}
}
我看了下源码 这个方法到API24都能用。。但不保证支持所有版本和机型!这里只是提供一些思路。。