From 691acaf67e4814b83f539ac9a35bdd1d800a24c9 Mon Sep 17 00:00:00 2001 From: liuguijing <1440265357@qq.com> Date: Mon, 10 Mar 2025 13:44:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96=E7=9F=AD?= =?UTF-8?q?=E4=BF=A1=E8=8E=B7=E5=8F=96sim=E5=8D=A1=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E7=83=AD=E7=82=B9=E5=BC=80=E5=90=AF=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=8B=EF=BC=8Cip=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=83=AD=E7=82=B9=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xypower/common/NetworkUtils.java | 31 ++++++++++++++++++- .../com/xypower/mpmaster/sms/SimUtil.java | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/com/xypower/common/NetworkUtils.java b/common/src/main/java/com/xypower/common/NetworkUtils.java index 58065629..97ba3971 100644 --- a/common/src/main/java/com/xypower/common/NetworkUtils.java +++ b/common/src/main/java/com/xypower/common/NetworkUtils.java @@ -6,14 +6,20 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.ConnectivityManager; +import android.net.LinkProperties; +import android.net.Network; +import android.net.NetworkCapabilities; import android.net.NetworkInfo; import android.net.Uri; +import android.net.wifi.WifiManager; import android.telephony.TelephonyManager; import android.text.TextUtils; +import android.text.format.Formatter; import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; +import java.net.SocketException; import java.net.URI; import java.util.Enumeration; import java.util.regex.Pattern; @@ -55,7 +61,7 @@ public class NetworkUtils { } public static String getMobileNetworkIp(Context context) { - ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); + ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); @SuppressLint("MissingPermission") NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo(); if (networkInfos == null || networkInfos.length == 0) { @@ -89,6 +95,29 @@ public class NetworkUtils { } + public static String getMobileIPAddress() { + try { + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + while (interfaces.hasMoreElements()) { + NetworkInterface networkInterface = interfaces.nextElement(); + if (networkInterface.isUp() && !networkInterface.isLoopback()) { + if (networkInterface.getName() != null && !networkInterface.getName().contains("ap")) { + Enumeration addresses = networkInterface.getInetAddresses(); + while (addresses.hasMoreElements()) { + InetAddress address = addresses.nextElement(); + if (!address.isLoopbackAddress() && address.getAddress().length == 4) { // IPv4 + return address.getHostAddress(); + } + } + } + } + } + } catch (SocketException e) { + e.printStackTrace(); + } + return null; + } + public static int addAPN(Context context, String name, String desc, String numeric, String user, String pwd) { int id = -1; String NUMERIC = getSIMInfo(context); diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java index 801a19fe..63d6ea51 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java @@ -687,7 +687,7 @@ public class SimUtil { result.append(isActiveSlot ? ",默认" : ""); if (isActiveSlot) { - result.append(",IP=" + NetworkUtils.getMobileNetworkIp(context)); + result.append(",IP=" + NetworkUtils.getMobileIPAddress()); } result.append(" ");