有一个项目,要控制设备wifi连接,而且要使用静态ip,上网查找了下,基本都是Android2.X下面的方法,即使用Settings进行设置,但是这种设置方法对于Android3.X以上设备是无效的,通过研究在Android手机上手动设置静态ip,我们可以发现:

1、Android2.X系统下面,静态IP地址是一个全局设置,即设置好之后会对你以后连接的所有热点生效,都会使用你设置的IP地址

2、Android3.X以上系统,设置静态ip是针对每一个点连接的配置进行的,在热点1上的设置不会影响热点2

ok,基本明白了,Android2.X使用Settings进行设置,是一个全局设定,而之后的系统做了修改,更改了设置模式,将静态ip设置与每一个热点挂钩,那会不会是在热点的配置类里面实现的呢,为了验证,我们查看了Android4.0的sdk源码,分析完WifiConfiguration.java后,就明白了,里面有“ipAssignment”,“linkProperties”,"mRoutes"(3.x系统为“mGateways”)等多个新增的hidden项,看来只能通过反射的方法调用了,呵呵,下面上代码:

[java] view plaincopy

01.//******************************************************************************/

02.    //以下代码为android3.0及以上系统设置静态ip地址的方法

[java] view plaincopy

01.

[java] view plaincopy

01./*设置ip地址类型 assign:STATIC/DHCP 静态/动态*/

[java] view plaincopy

01.    private static void setIpAssignment(String assign, WifiConfiguration wifiConf)

02.        throws SecurityException, IllegalArgumentException,

03.        NoSuchFieldException, IllegalAccessException {

04.    setEnumField(wifiConf, assign, "ipAssignment");

05.}

06.

07.@SuppressWarnings("unchecked")

[java] view plaincopy

01./*设置ip地址*/

[java] view plaincopy

01.    private static void setIpAddress(InetAddress addr, int prefixLength,

02.            WifiConfiguration wifiConf) throws SecurityException,

03.            IllegalArgumentException, NoSuchFieldException,

04.            IllegalAccessException, NoSuchMethodException,

05.            ClassNotFoundException, InstantiationException,

06.            InvocationTargetException {

07.        Object linkProperties = getField(wifiConf, "linkProperties");

08.        if (linkProperties == null)

09.            return;

10.        Class> laClass = Class.forName("android.net.LinkAddress");

11.        Constructor> laConstructor = laClass.getConstructor(new Class[] {

12.                InetAddress.class, int.class });

13.        Object linkAddress = laConstructor.newInstance(addr, prefixLength);

14.

15.        ArrayListmLinkAddresses = (ArrayList) getDeclaredField(

16.                linkProperties, "mLinkAddresses");

17.        mLinkAddresses.clear();

18.        mLinkAddresses.add(linkAddress);

19.    }

20.

21.        @SuppressWarnings("unchecked")/*设置网关*/

22.    private static void setGateway(InetAddress gateway,

23.            WifiConfiguration wifiConf) throws SecurityException,

24.            IllegalArgumentException, NoSuchFieldException,

25.            IllegalAccessException, ClassNotFoundException,

26.            NoSuchMethodException, InstantiationException,

27.            InvocationTargetException {

28.        Object linkProperties = getField(wifiConf, "linkProperties");

29.        if (linkProperties == null)

30.            return;

31.

32.        if (android.os.Build.VERSION.SDK_INT >= 14) { // android4.x版本

33.            Class> routeInfoClass = Class.forName("android.net.RouteInfo");

34.            Constructor> routeInfoConstructor = routeInfoClass

35.                    .getConstructor(new Class[] { InetAddress.class });

36.            Object routeInfo = routeInfoConstructor.newInstance(gateway);

37.

38.            ArrayListmRoutes = (ArrayList) getDeclaredField(

39.                    linkProperties, "mRoutes");

40.            mRoutes.clear();

41.            mRoutes.add(routeInfo);

42.        } else { // android3.x版本

43.            ArrayListmGateways = (ArrayList) getDeclaredField(

44.                    linkProperties, "mGateways");

45.            mGateways.clear();

46.            mGateways.add(gateway);

47.        }

48.

49.    }

50.

51.

52.        @SuppressWarnings("unchecked")/*设置域名解析服务器*/

53. private static void setDNS(InetAddress dns, WifiConfiguration wifiConf)throws SecurityException, IllegalArgumentException,NoSuchFieldException, IllegalAccessException

54. {Object linkProperties = getField(wifiConf, "linkProperties");if (linkProperties == null)return;ArrayListmDnses = (ArrayList) getDeclaredField(linkProperties, "mDnses");mDnses.clear(); // 清除原有DNS设置(如果只想增加,不想清除,词句可省略)mDnses.add(dns);

55. //增加新的DNS}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

56. 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({

57. "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)

58. f.getType(), value));} //***以上是android3.x以上设置静态ip地址的方法*********************************************************************************

59.

60.

61.

62.

63.

下面是调用方法:

64.private void setIpWithTfiStaticIp() {

65.

66.        WifiConfiguration wifiConfig;

67.

68.        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

69.        WifiInfo connectionInfo = wifiManager.getConnectionInfo();

70.        ListconfiguredNetworks = wifiManager

71.                .getConfiguredNetworks();

72.        for (WifiConfiguration conf : configuredNetworks) {

73.            if (wifiConf.networkId == connectionInfo.getNetworkId()) {

74.                WifiConf = conf;

75.                break;

76.            }

77.        }

78.

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

80.

81.            ContentResolver ctRes = context.getContentResolver();

82.            Settings.System

83.                    .putInt(ctRes, Settings.System.WIFI_USE_STATIC_IP, 1);

84.            Settings.System.putString(ctRes, Settings.System.WIFI_STATIC_IP,

85.                    "192.168.0.202");

86.            Settings.System.putString(ctRes,

87.                    Settings.System.WIFI_STATIC_NETMASK, "255.255.255.0");

88.            Settings.System.putString(ctRes,

89.                    Settings.System.WIFI_STATIC_GATEWAY, "192.168.0.1");

90.            Settings.System.putString(ctRes, Settings.System.WIFI_STATIC_DNS1,

91.                    "192.168.0.1");

92.            Settings.System.putString(ctRes, Settings.System.WIFI_STATIC_DNS2,

93.                    "61.134.1.9");

94.

95.        } else { // 如果是android3.x版本及以上的话

96.            try {

97.                setIpAssignment("STATIC", wifiConfig);

98.                setIpAddress(InetAddress.getByName("192.168.0.202"), 24,

99.                        wifiConfig);

100.                setGateway(InetAddress.getByName("192.168.0.1"), wifiConfig);

101.                setDNS(InetAddress.getByName("192.168.0.1"), wifiConfig);

102.                wifiManager.updateNetwork(wifiConfig); // apply the setting

103.                System.out.println("静态ip设置成功!");

104.            } catch (Exception e) {

105.                e.printStackTrace();

106.                System.out.println("静态ip设置失败!");

107.            }

108.        }

109.

110.    }

android 设置静态ip,Android下用代码设置静态IP地址的方法(完美支持Android2.X,Android3.X,Android4.X)...相关推荐

  1. android设置屏幕高度和宽度设置,Android手机的屏幕宽高度和代码设置控件的宽高度...

    1.Android手机的屏幕宽高度 WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); int w ...

  2. linux mysql 客户端编码设置_mac和linux下mysql字符集设置问题

    为什么要设置字符集 设置字符集主要是解决乱码问题,由于中文和英文编码不同导致,中文出现乱码,所以一般都设置为utf8格式 不同的字符集占用的字节大小不同,选择合适的字符集可以提高数据库的性能, mac ...

  3. outlook2010签名设置 怎麼用html 设置名片,windows10系统下Outlook2010如何设置邮箱签名...

    经常使用邮箱的朋友们,都会设置一个邮箱签名.而邮箱签名就是在你发出的每封邮箱后面都会加上你的签名档内容,方便邮件接收者了解你的基本信息,相当于个人名片.那么,windows10系统下Outlook20 ...

  4. java ip 白名单_Java代码中对IP进行白名单验证

    public classipUtil {//IP的正则,这个正则不能验证第一组数字为0的情况//private static Pattern pattern = Pattern//.compile(& ...

  5. linux定时更换无规则ip,Linux下使用keepalived实现虚IP的切换

    华为云VPC有个特性是虚IP(Virtual IP),虚IP的功能类似于浮动IP,可以绑定到多个ECS上,但是该特性很容易受到客户使用上的困惑,以为VIP绑定了到多个ECS上就能自动配置,其实不然,V ...

  6. html实现静态下来菜单js,JS代码实现静态导航菜单效果要用何主要代码?

    [实例代码]html xmlns=http://www.w3.org/1999/xhtml headtitle标题页-学无忧(www.xue51.com)/title/headBODY bgcolor ...

  7. linux pcre静态编译,Linux下,Nginx部署静态网站

    1.准备工作 选首先安装这几个软件:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL. Nginx是C写的,需要用GCC编译:Ngin ...

  8. ffmpeg 设置网络代理_MAC下使用SSH设置代理的办法

    发表于 2013-04-21 13:04:38 by 月小升 MAC下可以利用这个SSH翻qiang 然后,打开"系统偏好设置"->"网络"->&q ...

  9. 电视android界面卡,电视盒子画面卡顿怎么办?这三个方法完美解决困扰

    进入 2020 年,不知道有多少家里还在用几年前购买的电视盒子,其实电视盒子就和我们的手机一样,虽然使用频率相比较手机而言没有那么频繁,但随着近些年的发展,智能电视软件越来越大,对电视盒子的配置需要也 ...

最新文章

  1. 消除8个关于AI在商业中应用的错误观念
  2. iOS开发-NSArray
  3. NumericUpDown 控件输入限制小数位
  4. 八十六、Spring Cloud Consul:服务治理与配置中心
  5. SpringSecurity OAuth2在项目中使用完成的功能说明
  6. 无法显示论坛的登陆验证码
  7. 英特尔预计第12代酷睿H系列处理器将有超过100款设备采用
  8. 2017-2018-1 《信息安全系统设计基础》课程总结
  9. IDEA(2018)连接MySQL数据库失败的解决方法(报错08001)
  10. 【Web 开发】第1章 概论
  11. win10系统任务栏不显示最小化窗口的处理步骤
  12. 去哪儿CEO庄辰超:傍百度战携程与巨头共舞
  13. win10环境下创建环境变量
  14. 苹果手机很卡怎么解决_iPhone很卡怎么办,教您如何解决iPhone很卡问题!
  15. 宁夏中卫市:新一代云计算走向世界
  16. 回顾丨2022隐私计算融合区块链技术论坛(附视频+演讲PPT)
  17. 非全也要卷?复旦大学软件非全很多高分
  18. 用计算机探索ppt,信息技术应用 用计算机画函数图象ppt课件配套教案内容
  19. 无线衰落信道的分类方式和选择性衰落条件
  20. webview 禁用横竖屏切换_Android 禁止横竖屏切换

热门文章

  1. 【Web前端】【jquery】jquery实战:cookie、AJAX、放大镜
  2. B02.有意思的小东西 - 辅助英文阅读【福利】
  3. CAD模型减面和轻量化应用 不必重建数模
  4. Android 实现Json数据解析,并进行应用!
  5. Docker学习之五:Docker本地存储Volumes
  6. 平安WiFi举办合作伙伴沟通会,生态系统将成为全新突破口
  7. matlab笔记本8g够吗,【求助】Surface Pro 4,i5 4G 128G还是i5 8G 256G? - 笔记本电脑(Notebook)版 - 北大未名BBS...
  8. sql server 与 oracle 数据库连接
  9. Pico neo3在Unity中的交互操作
  10. 360Vedio To NFOV Vedio