在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地址:
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