//操作 盒子WiFi 开关的方法 false 表示关闭  true 表示打开public static boolean wifiSwitch(Context context,boolean open){WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);if (wifiManager.isWifiEnabled() && !open) {wifiManager.setWifiEnabled(false);} else if (!wifiManager.isWifiEnabled() && open){wifiManager.setWifiEnabled(true);}return true;}//设置盒子 上网路由方式  static/DHCPpublic static void setStaticIP(Context context,String type,String ip) {EthernetManager mEthManager = (EthernetManager) context.getSystemService(Context.ETHERNET_SERVICE);WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);switch (type) {case "Static":StaticIpConfiguration mStaticIpConfiguration = new StaticIpConfiguration();String[] split = getDns(HwContext.getContext()).split(" / ");for (int i = 0; i < split.length && !TextUtils.isEmpty(split[i]); i++) {InetAddress inetAddress=android.net.NetworkUtils.numericToInetAddress(split[i]);mStaticIpConfiguration.dnsServers.add(inetAddress);}Inet4Address inetAddr = getIPv4Address(ip);int prefixLength = NetUtils.maskStr2InetMask(NetworkUtils.getLanMask(HwContext.getContext()));InetAddress gatewayAddr = getIPv4Address(NetworkUtils.getDefaultGateway(HwContext.getContext()));if (inetAddr.getAddress().toString().isEmpty() || prefixLength ==0 ) {return ;}mStaticIpConfiguration.ipAddress = new LinkAddress(ip+"/"+prefixLength);mStaticIpConfiguration.gateway=gatewayAddr;switch (NetworkUtils.getActiveNetworkType(getContext())) {case "Ethernet":LogUtils.e("Ethernet");IpConfiguration mIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null);mEthManager.setConfiguration("eth0",mIpConfiguration);break;case "WiFi":LogUtils.e("\"WiFi\"");IpConfiguration wifiIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null);WifiConfiguration wifiConfiguration = getCurrentConfig();wifiConfiguration.setIpConfiguration(wifiIpConfiguration);wifiManager.setWifiApConfiguration(wifiConfiguration);wifiManager.updateNetwork(wifiConfiguration);new Thread(){// 这里比较耗时 所以开启新线程进行处理@Overridepublic void run() {super.run();int netId = wifiManager.addNetwork(wifiConfiguration);wifiManager.disableNetwork(netId);boolean b = wifiManager.enableNetwork(netId, true);}}.start();break;}break;case "DHCP":LogUtils.e("DHCP");switch (NetworkUtils.getActiveNetworkType(getContext())){case "Ethernet":LogUtils.e("Ethernet");IpConfiguration ipConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.DHCP, IpConfiguration.ProxySettings.NONE, null, null);mEthManager.setConfiguration("eth0",ipConfiguration);break;case "WiFi":LogUtils.e("DHCP");IpConfiguration wifiIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.DHCP, IpConfiguration.ProxySettings.NONE, null, null);WifiConfiguration wifiConfiguration = getCurrentConfig();wifiConfiguration.setIpConfiguration(wifiIpConfiguration);wifiManager.setWifiApConfiguration(wifiConfiguration);wifiManager.updateNetwork(wifiConfiguration);new Thread(){// 这里比较耗时 所以开启新线程进行处理@Overridepublic void run() {super.run();int netId = wifiManager.addNetwork(wifiConfiguration);wifiManager.disableNetwork(netId);boolean b = wifiManager.enableNetwork(netId, true);}}.start();break;}break;}}
public static int maskStr2InetMask(String maskStr) {StringBuffer sb ;String str;int inetmask = 0;int count = 0;/** check the subMask format*/Pattern pattern = Pattern.compile("(^((\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$)|^(\\d|[1-2]\\d|3[0-2])$");if (pattern.matcher(maskStr).matches() == false) {Log.e("maskStr2InetMask","subMask is error");return 0;}String[] ipSegment = maskStr.split("\\.");for(int n =0; n<ipSegment.length;n++) {sb = new StringBuffer(Integer.toBinaryString(Integer.parseInt(ipSegment[n])));str = sb.reverse().toString();count=0;for(int i=0; i<str.length();i++) {i=str.indexOf("1",i);if(i==-1)break;count++;}inetmask+=count;}return inetmask;}public static Inet4Address getIPv4Address(String text) {try {return (Inet4Address) NetworkUtils.numericToInetAddress(text);} catch (IllegalArgumentException|ClassCastException e) {return null;}}

AndroidMenifest文件里添加一下 配置

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.NETWORK_STATE" /><uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CONNECTIVITY_INTERNAL"/><uses-permission android:name="android.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS"/><uses-permission android:name="android.permission.OVERRIDE_WIFI_CONFIG"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

前提条件  导入 classes.jar  参考

如何访问 android系统hide的类或接口_菜鸟xiaowang的博客-CSDN博客

Android 修改盒子路由方式 Static / DHCP相关推荐

  1. 修改android默认锁屏方式

    代码为公司的定制版基于android5.1,没有具体比较锁屏部分应该和原生代码有所不同.本文基于浏览代码结果,没有手机版本和代码版本不一致(APP上还是一脉相承)所以没有在手机上直接试验. 首先在自己 ...

  2. android菜单更改,Android修改分析:删除设置中菜单

    Android修改分析:删除设置中菜单 设置中的菜单主要分为两种添加方式: `` 1.静态加载 所谓自主添加,就是通过xml文件,自主通过添加布局到Fragment.Activity等加载到设置菜单中 ...

  3. 路由工作原理+DHCP+静态路由配置

    路由工作原理+DHCP+静态路由配置 路由器 路由表 路由优先级 路由优先级的配置 浮动路由 路由的度量 DHCP DHCP第一种配置命令(global): DHCP第二种分配IP地址的配置命令(in ...

  4. Android项目解耦--路由框架ARouter的使用

    Android项目解耦–路由框架ARouter源码解析 前言 随着业务量的增长,客户端必然随之越来越业务和功能模块耦合越来越生,开发人员代码维护成本越来越高. App一般都会走向组件化.插件化的道路, ...

  5. Android 的媒体路由功能应用与框架解析 MediaRouter

    一.功能描述 Android的媒体路由API被设计用来允许多种媒体(视频.音乐.图片)在与ANDROID设备连接(无线或有线)的辅助设备(如电视.立体声.家庭戏院系统.音乐播放机)上显示和播放,使用该 ...

  6. Android 的媒体路由功能应用与框架解析

    一.功能描述 Android 的媒体路由API被设计用来允许多种媒体(视频.音乐.图片)在与ANDROID设备连接(无线或有线)的辅助设备(如电视.立体声.家庭戏院系统.音乐播放机)上显示和播放,使用 ...

  7. Android验证码倒计时实现方式总结

    Android验证码倒计时实现方式总结 几乎所有的APP里面都有验证码倒计时按钮,自己在项目中也尝试,简单总结为以下三种: 1.使用线程和Handler的方式,定时刷新倒计时数字,这种方式容易导致内存 ...

  8. Android实现截屏方式

    本文介绍了Android 实现截屏方式整理,分享给大家.希望对大家有帮助 可能的需求: 截自己的屏 截所有的屏 带导航栏截屏 不带导航栏截屏 截屏并编辑选取一部分 自动截取某个空间或者布局 截取长图 ...

  9. Android中的IPC方式

    Android中的IPC方式 在上一篇,我们探索了IPC的基本知识,序列化,和Binder的基本实现原理 本篇我们来看看Android中的一些具体的跨进程通信的方式 使用Bundle 我们知道,四大组 ...

最新文章

  1. RedHat8 配置本地yum源
  2. python怎么使用time模块_PYTHON的TIME模块使用
  3. python, 面向对象编程Object Oriented Programming(OOP)
  4. LA 3263 That Nice Euler Circuit (2D Geometry)
  5. hive explode函数入门及其与lateral view的结合
  6. 面试官:什么是对象池?有什么用?别说你还不会!
  7. java 获取当前文件的路径+文件全名
  8. python实验九答案_Python程序设计实验报告:实验九 python 包管理
  9. 黑马程序员顺义校区php_2018黑马整套php视频教程
  10. windows对应字体
  11. 英特尔首席工程师吴甘沙:一切弯路都是直路
  12. Paraview:Python Calculator Filter
  13. adobe animate2022动画制作软件
  14. 在idea中启动tomcat项目报错提示“java.util.zip.ZipException: error in opening zip file“
  15. linux下修改tomcat默认访问主页
  16. manjaro Linux yay常用命令整理大全
  17. SpringBoot上传大文件并支持中途取消上传
  18. PD的CDM模型中的三种实体关系
  19. 软件保护工具VMProtect用户手册——添加和搜索水印
  20. GROMACS中mdp文件注解小结

热门文章

  1. 面试必问 | 如何设计一款高并发的消息中间件?
  2. 我的世界国际版仿java版材质包_我的世界国际版如何更换材质包和光影
  3. 金错刀对话口袋购物王珂:找到痛点,确认卖点,制造爆点! - 资讯 - i黑马网...
  4. joint绘制流程图
  5. 帆软JS图标实现自动炫酷滚动效果
  6. html canvas生成图片,html5 canvas画板涂鸦生成图片代码
  7. C++知识点总结(一)
  8. springcloud~feign POST form-url-encoded data
  9. 大可乐android 4.3刷机包,国产Android 4.2:
  10. bs文件服务器,BS架构(示例代码)