Android 判断网络是否可用 & 获取IP地址 & 获取以太网口MAC地址

判断网络是否可用:

注意!是判断网络是否可用,但网络可用不代表一定能上外网的!


    public static boolean isNetworkAvailable(Context context) {

        ConnectivityManager manager = (ConnectivityManager) context
                .getApplicationContext().getSystemService(
                        Context.CONNECTIVITY_SERVICE);
        if (manager == null) {
            return false;
        }
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected();
        
    }

在AndroidManifest.xml文件下,添加下面权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

如果想判断是否能上外网可以ping一下外网的ip地址:

    public static boolean pingIPAddress(String ipAddress) {
        try {
            //-c 1是指ping的次数为1次,-w 3是指超时时间为3s
            Process process = Runtime.getRuntime()
                    .exec("ping -c 1 -w 3 " + ipAddress);
            //status为0表示ping成功
            int status = process.waitFor();
            if (status == 0) {
                return true;
            }
        }catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }
        return false;
    }

比如ping一下qq.com:

pingIPAddress("qq.com");

在AndroidManifest.xml文件下,添加下面权限:

<uses-permission android:name="android.permission.INTERNET"/>

获取IP地址:

	public static String getHostIPAddress() {
        String IPAddress = null;
        try {
            Enumeration nis = NetworkInterface.getNetworkInterfaces();
            while (nis.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) nis.nextElement();
                Enumeration<InetAddress> ias = ni.getInetAddresses();
                while (ias.hasMoreElements()) {
                    InetAddress ia = ias.nextElement();
                    if (ia instanceof Inet6Address) {
                        continue;
                    }
                    String hostAddress = ia.getHostAddress();
                    if (!"127.0.0.1".equals(hostAddress)) {
                        IPAddress = hostAddress;
                        break;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        
        return IPAddress;
    }

在AndroidManifest.xml文件下,添加下面权限:

<uses-permission android:name="android.permission.INTERNET"/>

获取以太网口MAC地址:


    public static String getEthernetMacAddress() {
        BufferedReader reader = null;
        FileReader fr = null;
        String ethernetMacAddress = null;
        try {
            fr = new FileReader("sys/class/net/eth0/address");
            reader = new BufferedReader(fr);
            ethernetMacAddress = reader.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null)
                    reader.close();
                if (fr != null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ethernetMacAddress;
    }
    
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取有线网络MAC地址IP地址可以使用以下代码: ```java // 获取MAC地址 WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); String macAddress = wifiInfo.getMacAddress(); // 获取有线网络MAC地址 try { NetworkInterface networkInterface = NetworkInterface.getByName("eth0"); byte[] macBytes = networkInterface.getHardwareAddress(); if (macBytes != null) { StringBuilder stringBuilder = new StringBuilder(); for (byte b : macBytes) { stringBuilder.append(String.format("%02X:", b)); } if (stringBuilder.length() > 0) { stringBuilder.deleteCharAt(stringBuilder.length() - 1); } String macAddress = stringBuilder.toString(); } } catch (SocketException e) { e.printStackTrace(); } // 获取IP地址 WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); int ipAddress = wifiManager.getConnectionInfo().getIpAddress(); String ip = Formatter.formatIpAddress(ipAddress); // 获取有线网络IP地址 try { Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface networkInterface = enumeration.nextElement(); Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses(); while (inetAddressEnumeration.hasMoreElements()) { InetAddress inetAddress = inetAddressEnumeration.nextElement(); if (!inetAddress.isLinkLocalAddress() && !inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { String ip = inetAddress.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } ``` 需要注意的是,获取有线网络MAC地址需要使用 `NetworkInterface` 类。由于不同的设备可能有不同的网络名称,因此需要根据实际情况来确定接名称。在上面的代码中,我使用了 `eth0` 这个接名称,这个名称在大多数Android设备上都是有效的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值