宁德阿里云代理商: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

相关推荐

  • 阿里云企业邮箱:阿里云的邮件恢复为何更迅速?

    阿里云企业邮箱:为何邮件恢复速度能快人一步? 在商业协作中,一封关键邮件的丢失可能意味着重大损失。当传统邮箱需要数小时甚至更久才能恢复数据时,阿里云企业邮箱却能做到分钟级恢复。这背后的技术逻辑是什么?让我们通过实际使用体验揭开其高速恢复的奥秘。 一、底层架构的三大核心技术优势 分布式存储引擎 采用自研飞天系统分布式架构,邮件数据实时同步至三个物理隔离的存储节…

    2025年6月23日
    46500
  • 阿里云服务器干嘛用的

    阿里云服务器是阿里云提供的一种云计算产品,用于存储、托管和运行各种应用程序和服务。阿里云服务器可以用来搭建和管理网站、应用程序、数据库、游戏服务器等。 具体用途包括但不限于: 网站托管:用来存储和部署网站的文件、数据库和应用程序,使网站可以通过互联网访问。 应用程序部署:用来部署和运行各种应用程序,如企业管理系统、客户关系管理软件等。 数据存储:用来存储和管…

    2023年10月25日
    67700
  • 苏州阿里云代理商:阿里云自动化部署方案

    苏州的阿里云代理商提供了专业的阿里云自动化部署方案,该方案可以帮助企业实现快速、可靠的应用部署,提高工作效率和降低成本。 我们的自动化部署方案包括以下主要内容: 自动化部署工具:我们提供各种自动化部署工具,如Ansible、Chef、Puppet等,可以帮助企业快速部署应用和配置环境。 自动化流程设计:我们根据客户的需求和现有环境,设计合适的自动化部署流程,…

    2024年2月24日
    64400
  • 如何查看阿里云服务器开放端口号码

    怎么查看阿里云主机服务器开放了哪些端口 1.Windows可以在cmd窗口下输入命令:netstat -abn查看端口监听情况和对应的进程。2.如果是linux,请输入命令:netstat -apn查看。希望可以帮到你! 怎么查看阿里云主机开放了哪些端口 自己的主机,可以在开启内网服务监听端口后,去看下防火墙和安全组是否允许该端口接受公网的访问。别人的主机,…

    2023年8月30日
    66800
  • 阿里云企业邮箱:如何优化阿里云邮件存储成本?

    阿里云企业邮箱:如何优化阿里云邮件存储成本? 一、阿里云企业邮箱的核心优势 阿里云企业邮箱作为国内领先的云邮件服务,具备以下核心优势: 高可靠性:基于阿里云全球基础设施,提供99.9%的服务可用性保障。 弹性扩展:存储空间可按需扩容,避免资源浪费。 智能安全防护:内置反垃圾邮件、病毒查杀和敏感信息过滤功能。 多终端协同:支持PC、移动端无缝同步,提升办公效率…

    2025年8月11日
    44800

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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