Skip to content

Commit

Permalink
优化以太网ip获取
Browse files Browse the repository at this point in the history
  • Loading branch information
ichtj committed Mar 26, 2024
1 parent 9858a54 commit 3df5d3b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/ichtj/basetools/OptionAty.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,23 @@ public static String getEthIPv4Address() {
}
}
} else {
// For versions before Android M, there is no direct API to get Ethernet information.
// You may need to use reflection or other methods to check Ethernet connectivity.
//Log.w(TAG, "Android version is too low to directly get Ethernet information.");
try{
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
if (networkInterface.getName().equals("eth0") || networkInterface.getName().equals("wlan0")) { // 可能是eth0或wlan0
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) {
return inetAddress.getHostAddress();
}
}
}
}
}catch(Throwable throwable){
throwable.printStackTrace();
}
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion config.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ext {
//选择对应签名 "cainiao" "rockchip" "imx8" "freescale"
BUILD_SIGN="rockchip"
BUILD_SIGN="freescale"
BUILD_SIGN_PWD="android"
BUILD_SIGN_PATH="../other/facefuture.jks"
//android开发版本配置
Expand Down

0 comments on commit 3df5d3b

Please sign in to comment.