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()]);
    }
    
}

以下是一个获取以太网信息的示例代码: ``` public class EthernetInfoActivity extends AppCompatActivity { private TextView tvIpMode; private TextView tvIpAddress; private TextView tvGateway; private TextView tvSubnetMask; private TextView tvDns1; private TextView tvDns2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ethernet_info); tvIpMode = findViewById(R.id.tv_ip_mode); tvIpAddress = findViewById(R.id.tv_ip_address); tvGateway = findViewById(R.id.tv_gateway); tvSubnetMask = findViewById(R.id.tv_subnet_mask); tvDns1 = findViewById(R.id.tv_dns1); tvDns2 = findViewById(R.id.tv_dns2); // 获取以太网管理器 EthernetManager ethernetManager = (EthernetManager) getSystemService(Context.ETHERNET_SERVICE); // 获取IP模式 int ipMode = ethernetManager.getEthernetMode(); if (ipMode == EthernetManager.ETHERNET_CONNECT_MODE_DHCP) { tvIpMode.setText("DHCP"); } else if (ipMode == EthernetManager.ETHERNET_CONNECT_MODE_MANUAL) { tvIpMode.setText("静态IP"); } // 获取IP地址、网关、子网掩码、DNS EthernetDevInfo devInfo = ethernetManager.getSavedEthernetDevInfo(); if (devInfo != null) { tvIpAddress.setText(devInfo.getIpAddress()); tvGateway.setText(devInfo.getRouteAddr()); tvSubnetMask.setText(devInfo.getNetMask()); tvDns1.setText(devInfo.getDnsAddr()); tvDns2.setText(devInfo.getDns2Addr()); } } } ``` 在布局文件中,我们需要添加相应的TextView: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="IP模式:" /> <TextView android:id="@+id/tv_ip_mode" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="IP地址:" /> <TextView android:id="@+id/tv_ip_address" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="网关地址:" /> <TextView android:id="@+id/tv_gateway" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="子网掩码:" /> <TextView android:id="@+id/tv_subnet_mask" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首选DNS:" /> <TextView android:id="@+id/tv_dns1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="备选DNS:" /> <TextView android:id="@+id/tv_dns2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> ``` 运行该示例代码将会显示以太网的IP模式、IP地址、网关地址、子网掩码、首选DNS、备选DNS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值