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

来源:互联网 发布:淘宝达人链接怎么获取 编辑:程序博客网 时间:2024/06/06 09:52

转自:http://www.aichengxu.com/java/14061.htm

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 settingSystem.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// }


对自己很有用,所以,摘过来了!留作学习ing

0 0