宁德阿里云代理商:android获取网络ip

在Android上获取网络IP地址可以使用以下代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class NetworkUtils {
    
    public static String getIPAddress() {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress inetAddress = addresses.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    public static String getPublicIP() {
        String publicIP = null;
        try {
            Process process = Runtime.getRuntime().exec("curl -s ifconfig.me");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            publicIP = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return publicIP;
    }
}

然后,你可以在你的Android应用程序中调用getIPAddress()方法获取设备的IP地址,或者调用getPublicIP()方法获取公共IP地址。

示例用法:

String ipAddress = NetworkUtils.getIPAddress();
String publicIP = NetworkUtils.getPublicIP();

// 打印IP地址
System.out.println("设备IP地址: " + ipAddress);
System.out.println("公共IP地址: " + publicIP);

请注意,获取公共IP地址的方法使用了curl命令,因此需要确保你的设备上已经安装了curl

在Android中,要获取当前设备的网络IP地址,可以通过以下方法之一:

方法1. 使用Java代码获取IP地址:

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

public class NetworkUtils {

    public static String getIPAddress() {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress inetAddress = addresses.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }
}

然后在你的Activity或Fragment中调用 NetworkUtils.getIPAddress() 方法来获取IP地址:

String ipAddress = NetworkUtils.getIPAddress();

方法2. 使用Android系统的API获取IP地址:

宁德阿里云代理商:android获取网络ip
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;

public class NetworkUtils {

    public static String getDeviceIPAddress(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                Network network = cm.getActiveNetwork();
                if (network != null) {
                    NetworkInfo networkInfo = cm.getNetworkInfo(network);
                    if (networkInfo != null && networkInfo.isConnected()) {
                        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                            if (wifiManager != null) {
                                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                                if (wifiInfo != null) {
                                    return intIP2StringIP(wifiInfo.getIpAddress());
                                }
                            }
                        }
                    }
                }
            } else {
                NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
                if (activeNetwork != null && activeNetwork.isConnected()) {
                    if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                        if (wifiManager != null) {
                            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                            if (wifiInfo != null) {
                                return intIP2StringIP(wifiInfo.getIpAddress());
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

    private static String intIP2StringIP(int ip) {
        return (ip & 0xFF) + "." +
                ((ip >> 8) & 0xFF) + "." +
                ((ip >> 16) & 0xFF) + "." +
                ((ip >> 24) & 0xFF);
    }
}

在你的Activity或Fragment中调用 NetworkUtils.getDeviceIPAddress(context) 方法来获取IP地址(请确保传入的context不为null):

String ipAddress = NetworkUtils.getDeviceIPAddress(context);

这些方法可以获取到设备当前连接的网络的IP地址,比如通过Wi-Fi连接到路由器的IP地址。如果设备同时连接多个网络,可以根据需求进行调整。

发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/138069.html

(0)
luotuoemo的头像luotuoemo
上一篇 2024年2月5日 00:13
下一篇 2024年2月5日 00:28

相关推荐

  • 嘉兴阿里云代理商:阿里云的战略

    嘉兴阿里云代理商认为阿里云的战略主要分为以下几个方面: 打造全球领先的云计算平台:阿里云致力于提供全球领先的云计算技术和服务,通过不断创新和升级,提高云计算平台的性能、稳定性和安全性,为企业提供高效、可靠、安全的云计算服务。 深耕行业云计算:阿里云通过与各行各业的合作伙伴深耕行业,了解行业特点和需求,提供定制化的解决方案,帮助企业实现数字化转型,提升效率和竞…

    2024年2月10日
    75900
  • 榆林阿里云代理商:ajax 图片上传服务器

    榆林阿里云代理商可以使用Ajax技术来上传图片到服务器。下面是一个示例的代码: // HTML部分 <input type="file" id="imageUpload" /> <button onclick="uploadImage()">上传图片</button&g…

    2024年2月6日
    68400
  • 阿里云docker 镜像仓库

    阿里云docker镜像库免费吗 目前容器镜像服务处于公测期间,免让利或东轮九转输项校满费提供镜像托管、压茶承兴镜像构建服务以及镜像加速器服务,请放心使用。 来自官方计费说明。如需观末补市手南话查询请在阿里云>容器镜像服务>计费说明 查询 如何给Docker配置官方国内加速镜像 配置镜像加速器国内访问 Docker Hub 有时会遇到困难,此时可以…

    2023年8月27日
    81800
  • 阿里云企业邮箱:为什么医疗行业需邮件加密审计?

    阿里云企业邮箱:为什么医疗行业需邮件加密审计? 在数字化迅速发展的今天,医疗行业正经历前所未有的信息化转型。医疗机构通过电子邮件处理患者信息、诊疗记录、医学研究数据等敏感信息正在成为普遍现象。然而,这些数据的高度敏感性也导致医疗行业面对更为严峻的安全威胁。因此,医疗行业对邮件系统的安全性能提出了更高的要求,其中邮件加密和审计功能尤为关键。 医疗行业面临的邮件…

    2025年6月13日
    58300
  • 阿里云视频服务器异常怎么解决

    如果阿里云视频服务器出现异常,您可以尝试以下解决方法: 检查网络连接:确保您的网络连接正常,可以尝试重新连接一次。 检查服务器状态:登录阿里云控制台,检查服务器的状态是否正常,包括CPU、内存、存储空间等是否正常使用。 重启服务器:尝试重启服务器,有时候重启可以解决一些临时的问题。 检查视频服务配置:检查您的视频服务配置是否正确,包括域名解析、防火墙设置等。…

    2023年10月26日
    76400

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
购买阿里云服务器请访问:https://www.4526.cn/