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

相关推荐

  • 济南阿里云代理商:android 4.4 短信写入

    您好,要往Android 4.4系统中写入短信,可以使用以下代码: ContentValues values = new ContentValues(); values.put("address", "123456"); // 短信接收者的手机号 values.put("body", "H…

    2024年3月11日
    22000
  • 包头阿里云代理商:android发送短信权限

    包头阿里云代理商:android发送短信权限 介绍 Android是目前最流行的手机操作系统之一。在应用程序的开发过程中,有时我们需要使用手机发送短信的功能。然而,由于安全和隐私的原因,Android要求应用程序获得发送短信的权限。本文将介绍如何在Android应用程序中获取发送短信的权限,并结合阿里云的优势,探讨如何使用阿里云提供的服务来实现发送短信功能。…

    2024年1月20日
    31400
  • 阿里云 发短信

    阿里云 (Alibaba Cloud) 是一家由阿里巴巴集团旗下的云计算服务提供商。阿里云提供了多种云服务,其中包括短信服务 (SMS)。 使用阿里云的短信服务,可以通过API或控制台发送短信。以下是使用阿里云发送短信的基本步骤: 注册阿里云账号并登录阿里云控制台。 进入阿里云短信服务控制台,并开通短信服务。 在控制台中获取AccessKey ID和Acce…

    2023年10月26日
    26200
  • 太原阿里云代理商:aidl 拦截短信

    太原阿里云代理商可以利用AIDL(Android Interface Definition Language)拦截短信。AIDL是Android系统中用于实现进程间通信的一种机制,通过定义接口,可以实现在不同进程之间进行方法调用。 首先,在AndroidManifest.xml文件中声明一个Service组件,用于拦截短信。在Service的onBind方法…

    2024年2月16日
    27400
  • 企业员工对阿里云企业邮箱的新功能界面友好性反馈?

    企业员工对阿里云企业邮箱新功能界面友好性反馈 1. 界面简洁直观,用户易上手 阿里云企业邮箱的界面设计非常注重用户体验,整体风格简洁明了,操作流程简单,即使是非技术背景的员工也能够快速掌握基本操作。这种友好的设计使得新用户可以迅速上手,不需要过多培训便可高效使用。这对于企业来说,不仅减少了培训时间与成本,还提升了员工的工作效率,促进了信息传递的流畅性。 2.…

    2024年10月30日
    14300

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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