private void setIpWithTfiStaticIp() {
WifiConfiguration wifiConfig;
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = wifiManager
.getConfiguredNetworks();
for (WifiConfiguration conf : configuredNetworks) {
if (wifiConf.networkId == connectionInfo.getNetworkId()) {
WifiConf = conf;
break;
}
}
if (android.os.Build.VERSION.SDK_INT < 11) { // 如果是android2.x版本的话
ContentResolver ctRes = context.getContentResolver();
Settings.System.putInt(ctRes, Settings.System.WIFI_USE_STATIC_IP, 1);
Settings.System.putString(ctRes, Settings.System.WIFI_STATIC_IP, "192.168.0.202");
Settings.System.putString(ctRes, Settings.System.WIFI_STATIC_NETMASK, "255.255.255.0");
Settings.System.putString(ctRes, Settings.System.WIFI_STATIC_GATEWAY, "192.168.0.1");
Settings.System.putString(ctRes, Settings.System.WIFI_STATIC_DNS1, "192.168.0.1");
Settings.System.putString(ctRes, Settings.System.WIFI_STATIC_DNS2, "61.134.1.9");
} else { // 如果是android3.x版本及以上的话
try {
setIpAssignment("STATIC", wifiConfig);
setIpAddress(InetAddress.getByName("192.168.0.202"), 24, wifiConfig);
setGateway(InetAddress.getByName("192.168.0.1"), wifiConfig);
setDNS(InetAddress.getByName("192.168.0.1"), wifiConfig);
wifiManager.updateNetwork(wifiConfig); // apply the setting
System.out.println("静态ip设置成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("静态ip设置失败!");
}
}
}