android Wifi 设置静态ip地址的方法

调用setIpWithTfiStaticIp()即可为连接好的wifi配置 静态Ip。支持Android4.0以上及以下的版本。(PS:以下的函数使用条件是:wifi是连接好的)

测试成功的

  /**

     * 设置静态ip地址的方法
     */
    private boolean setIpWithTfiStaticIp() {  
    WifiManager wifiManager = (WifiManager) FactoryTestApp.context.getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration wifiConfig = null;
WifiInfo connectionInfo = wifiManager. getConnectionInfo();  //得到连接的wifi网络

List<WifiConfiguration> configuredNetworks = wifiManager
.getConfiguredNetworks();
for (WifiConfiguration conf : configuredNetworks) {
if (conf.networkId == connectionInfo.getNetworkId()) {
wifiConfig = conf;
break;
}
}


if (android.os.Build.VERSION.SDK_INT < 11) { // 如果是android2.x版本的话


ContentResolver ctRes = FactoryTestApp.context.getContentResolver();
android.provider.Settings.System.putInt(ctRes, 
android.provider.Settings.System.WIFI_USE_STATIC_IP, 1);
android.provider.Settings.System.putString(ctRes, 
android.provider.Settings.System.WIFI_STATIC_IP,"192.168.0.202");
// android.provider.Settings.System.putString(ctRes,
// android.provider.Settings.System.WIFI_STATIC_NETMASK, "255.255.255.0");
// android.provider.Settings.System.putString(ctRes,
// android.provider.Settings.System.WIFI_STATIC_GATEWAY, "192.168.0.1");
// android.provider.Settings.System.putString(ctRes, 
// android.provider.Settings.System.WIFI_STATIC_DNS1,"192.168.0.1");
// android.provider.Settings.System.putString(ctRes, 
// android.provider.Settings.System.WIFI_STATIC_DNS2,"61.134.1.9");


return true;
} else { // 如果是android3.x版本及以上的话
try {
setIpAssignment("STATIC", wifiConfig);
setIpAddress(InetAddress.getByName("192.168.3.102"), 24, wifiConfig);
// setGateway(InetAddress.getByName("192.168.3.1"), wifiConfig);
// setDNS(InetAddress.getByName("192.168.0.1"), wifiConfig);
wifiManager.updateNetwork(wifiConfig); // apply the setting
System.out.println("静态ip设置成功!");
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("静态ip设置失败!");
return false;
}
}
    }
    
    private static void setIpAssignment(String assign, WifiConfiguration wifiConf)
throws SecurityException, IllegalArgumentException,
NoSuchFieldException, IllegalAccessException {
setEnumField(wifiConf, assign, "ipAssignment");
}


private static void setIpAddress(InetAddress addr, int prefixLength,
WifiConfiguration wifiConf) throws SecurityException,
IllegalArgumentException, NoSuchFieldException,
IllegalAccessException, NoSuchMethodException,
ClassNotFoundException, InstantiationException,
InvocationTargetException {
Object linkProperties = getField(wifiConf, "linkProperties");
if (linkProperties == null)
return;
Class<?> laClass = Class.forName("android.net.LinkAddress");
Constructor<?> laConstructor = laClass.getConstructor(new Class[] {
InetAddress.class, int.class });
Object linkAddress = laConstructor.newInstance(addr, prefixLength);


ArrayList<Object> mLinkAddresses = (ArrayList<Object>) getDeclaredField(
linkProperties, "mLinkAddresses");
mLinkAddresses.clear();
mLinkAddresses.add(linkAddress);
}

private 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;
}
private 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;
}
@SuppressWarnings({"unchecked", "rawtypes" })
private 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));
}


// private static void setGateway(InetAddress gateway,WifiConfiguration wifiConf) 
// throws SecurityException,
// IllegalArgumentException, NoSuchFieldException,
// IllegalAccessException, ClassNotFoundException,
// NoSuchMethodException, InstantiationException,
// InvocationTargetException {
// Object linkProperties = getField(wifiConf, "linkProperties");
// if (linkProperties == null)
// return;
//
// if (android.os.Build.VERSION.SDK_INT >= 14) { // android4.x版本
// Class<?> routeInfoClass = Class.forName("android.net.RouteInfo");
// Constructor<?> routeInfoConstructor = routeInfoClass
// .getConstructor(new Class[] { InetAddress.class });
// Object routeInfo = routeInfoConstructor.newInstance(gateway);
//
// ArrayList<Object> mRoutes = (ArrayList<Object>)getDeclaredField(
// linkProperties, "mRoutes");
// mRoutes.clear();
// mRoutes.add(routeInfo);
// } else { // android3.x版本
// ArrayList<InetAddress> mGateways = (ArrayList<InetAddress>) getDeclaredField(
// linkProperties, "mGateways");
// mGateways.clear();
// mGateways.add(gateway);
// }
// }
//    
//    private static void setDNS(InetAddress dns, WifiConfiguration wifiConf)
//     throws SecurityException, IllegalArgumentException,
//     NoSuchFieldException, IllegalAccessException{
//     Object linkProperties = getField(wifiConf, "linkProperties");
//     if (linkProperties == null)
//     return;
//     ArrayList<InetAddress> mDnses = (ArrayList<InetAddress>) 
//     getDeclaredField(linkProperties, "mDnses");
//     mDnses.clear(); // 清除原有DNS设置(如果只想增加,不想清除,词句可省略)
//     mDnses.add(dns);
//     //增加新的DNS
//    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值