用*#*#4636#*#*看IP時卻顯示:ip addr not reachable,怎麼才能知道連著數據流量時自己手機的IP地址,手機是android的
使用GPRS
首先,設置用戶上網權限
其次,代碼如下
public String getLocalIpAddress()
{
try
{
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
{
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
{
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress())
{
return inetAddress.getHostAddress().toString();
}
}
}
}
catch (SocketException ex)
{
Log.e("WifiPreference IpAddress", ex.toString());
}
return null;
}