gpt4 book ai didi

Android如何获取网络/Wi-Fi IP

转载 作者:行者123 更新时间:2023-11-30 02:13:11 40 4
gpt4 key购买 nike

我有一个问题,我想获取我的移动 IP

是网络还是Wi-Fi

我也尝试过一些方法,但我不知道为什么我不能得到它

 public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLocalIpAddress();
}
public String getLocalIpAddress(){
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()){
Log.e("IP", inetAddress.getHostAddress().toString());
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e( "Error:" , ex.toString());
}
return null ;
}
}

这是我的日志

fe80::a3c8:4c03:154:8e1d%rmnet_usb0

这是我的 AndroidManifest

 <uses-permission android:name= "android.permission.INTERNET" />  
<uses-permission android:name= "android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE" />

最佳答案

Plz try this it may help you..?
public String getLocalIpAddress()
{
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception ex) {
Log.e("IP Address", ex.toString());
}
return null;
}

Add below permission in the manifest file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

关于Android如何获取网络/Wi-Fi IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29823741/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com