Android 获取当前wifi网络或者mobile网络的本地IPv4 的IP地址

package com.common.utils;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.text.format.Formatter;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import static android.content.Context.CONNECTIVITY_SERVICE;
import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.ConnectivityManager.TYPE_WIFI;

/**
 * Author: Pan
 * 2021/3/28
 * Description: 获取当前wifi网络或者mobile的IPv4 的IP地址
 */
public class IPUtil {

    private static final IPUtil instance = new IPUtil();

    private IPUtil() {
    }

    public static IPUtil getInstance() {
        return instance;
    }


    private String currentIP = "";

    public String getCurrentIP() {
        return currentIP;
    }

    private void setWifiIP() {
        WifiManager wifiManager = (WifiManager) app.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
        currentIP = Formatter.formatIpAddress(ipAddress);
        LogUtil.d(" wifi 网络IP:" + currentIP);
    }

    private void setMobileNetworkIP() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                Enumeration<InetAddress> inetAddresses = networkInterfaces.nextElement().getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress address = inetAddresses.nextElement();
                    if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
                        LogUtil.d(" 移动网络IP:" + address.getHostAddress());
                        currentIP = address.getHostAddress();
                        return;
                    }
                }
            }
        } catch (SocketException e) {
            LogUtil.e(e);
        }
    }

    private Application app;

    public void init(Application app) {
        if (this.app == null) {
            this.app = app;
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
            NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver();
            app.registerReceiver(networkChangeReceiver, intentFilter);
        }

    }

    private class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectionManager = (ConnectivityManager) app.getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isAvailable()) {
                switch (networkInfo.getType()) {
                    case TYPE_MOBILE:
                        setMobileNetworkIP();
                        break;
                    case TYPE_WIFI:
                        setWifiIP();
                        break;
                    default:
                        break;
                }
            }
        }
    }


}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值