安卓-获取以太网ip地址

本文档探讨了在安卓开发中遇到的 IpConfiguration 类使用问题,以及如何检查以太网连接状态和获取IP地址。通过示例代码展示了如何利用 ConnectivityManager 获取网络信息,检查以太网是否连接,并遍历 NetworkInterface 获取以太网IP地址。对于无法使用 IpConfiguration 的情况,可能涉及权限或系统级别的限制。
摘要由CSDN通过智能技术生成
在安卓开发文档中发现有IpConfiguration这个表示网络 IP 配置的类,但是不知道为什么不能使用。
private val manager: ConnectivityManager
    get() = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
/**
     * 以太网是否连接
     */
    private fun connect(): Boolean {
        val info = manager.activeNetworkInfo
        if (info?.type != ConnectivityManager.TYPE_ETHERNET) {
            logger.debug { "以太网未连接,请重试" }
            return false
        }
        if (!info.isAvailable) {
            logger.debug { "以太网不可用,请重试" }
            return false
        }
        logger.debug { "以太网连接成功" }
        return true
    }

    /**
     * 获取以太网ip地址
     */
    private fun ipAddress() {
        try {
            val etherInterface = NetworkInterface.getNetworkInterfaces()
            while (etherInterface.hasMoreElements()) {//判断是否有数据
                val element = etherInterface.nextElement()
                if (!element.isUp) {
                    continue
                }
                if (element.displayName != "eth0") {
                    continue
                }
                val addresses = element.inetAddresses
                while (addresses.hasMoreElements()) {
                    val subElement = addresses.nextElement()
                    if (subElement.isSiteLocalAddress) {
                        val ip = subElement.hostAddress
                        logger.debug { "ip地址$ip" }
                    }
                }
            }
        } catch (e: Exception) {
            logger.error(e) { "" }
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个获取以太网信息的示例代码: ``` 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、付费专栏及课程。

余额充值