当前位置: 首页 > news >正文

Android 获取网关 ip 和 DNS ip

参考下方 PingUtil.java 代码

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.text.format.Formatter;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.util.LinkedList;public class PingUtil {/*** 获取网关(API>=29)*/public static String getGateway(Context context) {WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();int gateway = dhcpInfo.gateway;return Formatter.formatIpAddress(gateway);}/*** 获取dns*/public static String getDns() {String[] dnsServers = getDnsFromCommand();// 组装StringBuffer sb = new StringBuffer();if (dnsServers != null) {sb.append(dnsServers[0]);  //注意这里只会返回1个dns服务器ip}return sb.toString();}//通过 getprop 命令获取private static String[] getDnsFromCommand() {LinkedList<String> dnsServers = new LinkedList<>();try {Process process = Runtime.getRuntime().exec("getprop");InputStream inputStream = process.getInputStream();LineNumberReader lnr = new LineNumberReader(new InputStreamReader(inputStream));String line = null;while ((line = lnr.readLine()) != null) {int split = line.indexOf("]: [");if (split == -1) continue;String property = line.substring(1, split);String value = line.substring(split + 4, line.length() - 1);if (property.endsWith(".dns")|| property.endsWith(".dns1")|| property.endsWith(".dns2")|| property.endsWith(".dns3")|| property.endsWith(".dns4")) {InetAddress ip = InetAddress.getByName(value);if (ip == null) continue;value = ip.getHostAddress();if (value == null) continue;if (value.length() == 0) continue;dnsServers.add(value);}}} catch (Exception e) {e.printStackTrace();}return dnsServers.isEmpty() ? new String[0] : dnsServers.toArray(new String[dnsServers.size()]);}}
http://www.lryc.cn/news/109289.html

相关文章:

  • Docker root用户的pip使用方法
  • 企业新片场排名如何优化
  • Database Name
  • git代码版本管理
  • k8s概念-ConfigMap
  • Mybatis 实体类属性名和表中字段名不一致怎么处理
  • CAS - 从AtomicInteger窥探CAS
  • micro-ros IMU ML 代码
  • 二十三种设计模式第二十四篇--访问者模式(完结撒花)
  • 月报总结|Moonbeam 7月份大事一览
  • 【2023.8】docker一键部署wvp-GB28181-pro和ZLMediaKit过程全记录
  • 【2023】字节跳动 10 日心动计划——第四关
  • 数据库与数据仓库的区别及关系
  • Emacs之设置行号前景颜色(字体颜色)/背景颜色/光标颜色/背景透明度(一百二十七)
  • 【hive经典指标,离线数仓指标,ADS层指标分析】最近7日内连续3日下单用户数
  • 线上java程序CPU及内存占用过高问题排查总结
  • c高级:day3
  • Java检查值是否存在于数组中的3种方法
  • python 连接oracle pandas以简化excel的编写和数据操作
  • Kubernetes高可用集群二进制部署(三)部署api-server
  • 【网络|TCP】三次握手、四次握手
  • 刷题笔记 day7
  • Tuxera NTFS2023Mac强大的Mac读写工具
  • ARM64 常见汇编指令学习 11 -- ARM 汇编宏 .macro 的学习
  • 数据库的分库分表
  • [Docker实现测试部署CI/CD----相关服务器的安装配置(2)]
  • LC-980. 不同路径 III(回溯)
  • 软件测试缺陷报告
  • vue js-table2excel 导出excel 可带多张图片
  • HTML 基础标签