调用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 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 mLinkAddresses = (ArrayList) 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)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 mRoutes = (ArrayList)getDeclaredField( // linkProperties, "mRoutes"); // mRoutes.clear(); // mRoutes.add(routeInfo); // } else { // android3.x版本 // ArrayList mGateways = (ArrayList) 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 mDnses = (ArrayList)  //     getDeclaredField(linkProperties, "mDnses"); //     mDnses.clear(); // 清除原有DNS设置(如果只想增加,不想清除,词句可省略) //     mDnses.add(dns); //     //增加新的DNS //    }

android设置wifiip地址,android Wifi 设置静态ip地址的方法相关推荐

  1. 如何设置CentOS 7获取动态及静态IP地址

    自动获取动态IP地址 1.输入"ip addr"并按回车键确定,发现无法获取IP(CentOS 7默认没有ifconfig命令),记录下网卡名称(本例中为ens33). 2.输入& ...

  2. linux设置静态ip地址_什么是静态IP地址,与动态IP地址比较以及如何为Windows和Linux设置?...

    linux设置静态ip地址 IP addresses are the core mechanism of Computer networks. The IP address is used to sp ...

  3. 怎样设置远程服务器地址,远程桌面的静态ip地址怎样设置?

    步骤一.记录.设置电脑IP地址 先记录下电脑上使用的静态IP地址.子网掩码.网关和DNS服务器地址信息:然后把电脑的IP地址,设置为自动获得(动态IP),如下图所示. 先记录电脑上的静态IP地址信息, ...

  4. 安卓9.0设置WIFI静态IP地址。

    简单的一个函数实现对wifi设置静态或动态IP地址. 一.需要的权限 <uses-permission android:name="android.permission.ACCESS_ ...

  5. 设置Ubuntu 20.04的静态IP地址

    引言:我们做嵌入式或者其他的项目时,有时候不免发现,Ubuntu的ip地址经常会改变,这个时候就需要我们手动配置静态IP了. 给Ubuntu设置一个静态IP地址有以下几个好处: 持久性:静态IP地址是 ...

  6. 如何在 Debian 11 上设置一个静态 IP 地址

    当你在电脑上安装一个新的操作系统时,DHCP服务器会给你分配一个动态IP地址.然而,在各种情况下,你可能需要在你的机器上设置一个静态IP地址,例如,当你正在托管一个网络服务器,或者任何服务需要一个IP ...

  7. 【OS】Linux环境下配置固定IP地址--静态IP地址(不能上外网问题)

    [OS]Linux环境下配置固定IP地址--静态IP地址(不能上外网问题) ---red hat重启网卡 service network restart /etc/rc.d/init.d/networ ...

  8. android wifi连接分配ip,android Wifi 设置静态ip地址的方法

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

  9. Android手机静态ip地址网关,安卓手机Android 4.0系统静态ip设置方法【详解】

    安卓系统Android 4.0推出之后,很多网友都进行了版本的更新,但是,一些更新了Android 4.0系统的用户,表示,自从版本升级之后,就不能使用静态IP,这是怎么回事呢?小编经过研究后发现,并 ...

最新文章

  1. 精品思维导图,流程图模板分享
  2. python语言有几种编程方式_零基础自学python语言,有哪几种方法?龟叔说:这套教程带你入门...
  3. Anu Has a Function CodeForces - 1300C(二进制位运算)
  4. SLVS-EC接口学习
  5. 【栈】【字符】匹配(jzoj 1612)
  6. Angular实现悬浮球组件
  7. 斜率优化dp学习笔记
  8. 2016-2017-2 20155309 南皓芯《java程序设计》第八周学习总结
  9. 开发中常用日志搜索技巧
  10. ORAN专题系列-5:5G O-RAN 一体式小基站硬件白盒化的参考架构
  11. maven常用命令大全(附详细解释)
  12. Android SDK使用迅雷下载方法
  13. 补点C#基础_022_json校验和json在线编辑器-bejson
  14. ppt密码忘了怎么解除,ppt权限密码怎么解开?
  15. 林轩田机器学习基石(Machine Learning Foundations)笔记(一)
  16. 在午睡,手机接连振动,是几个朋友用短信微信问我
  17. JavaScript 数组array 插入[ push() ] 与 删除[ splice() ]
  18. 马尔科夫不等式和坎泰利不等式的证明
  19. Vuforia Ground Plane 平面识别
  20. 什么是 MATLAB(矩阵实验室)?工作、功能和应用

热门文章

  1. LeetCode 59. 螺旋矩阵 II
  2. 记一次kafka集群频繁crash的排查过程
  3. Go调度程序:Ms,PsGs
  4. 有效的MongoDB索引
  5. HashSet 和 TreeSet 及 Map 集合的简单方法,含程序。
  6. android剪切 图片,android 剪切图片
  7. Rabbitmq 1.消息如何保障 100%投递成功?
  8. PHP mysql_real_escape_string() 函数防止数据库攻击
  9. Jeecg-Boot 快速生成前后端代码
  10. 实战系列-Spring Cloud微服务中三把利器Feign、Hystrix、Ribbon