Android的获取IP地址时,连接到蜂窝网络 [英] Getting IP address of Android when connected to Cellular Network
问题描述
有没有什么简单的办法,当通过移动数据网络连接到互联网来获取我的手机的IP地址。为了得到无线网络的IP地址,我使用下面这个简单的技术。
WifiManager WM =(WifiManager)getSystemService(WIFI_SERVICE);
串的ip = Formatter.formatIpAddress(wm.getConnectionInfo()getIpAddress());
有什么办法类似于上面获得移动数据网络的IP地址。
我用下面的code,但它返回的MAC地址,WiFi和蜂窝网络的IP地址,但我感兴趣的只是在蜂窝IP地址。
字符串ip地址= NULL;
尝试{
对于(枚举<&NetworkInterface的GT; EN = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){
NetworkInterface的INTF = en.nextElement();
为(枚举&所述;为InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements()){
InetAddress类InetAddress类= enumIpAddr.nextElement();
如果(!inetAddress.isLoopbackAddress()){
ip地址= inetAddress.getHostAddress()的toString()。
Log.i(Sarao5,ip地址);
}
}
}
}赶上(SocketException前){}
低于code使用我在应用程序中使用 -
公共静态字符串getDeviceIPAddress(布尔useIPv4){
尝试{
清单<&NetworkInterface的GT; networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
对于(的NetworkInterface的NetworkInterface:networkInterfaces){
清单<&的InetAddress GT; inetAddresses = Collections.list(networkInterface.getInetAddresses());
对于(InetAddress类InetAddress类:inetAddresses){
如果(!inetAddress.isLoopbackAddress()){
。字符串SADDR = inetAddress.getHostAddress()与toUpperCase();
布尔isIPv4 = InetAddressUtils.isIPv4Address(SADDR);
如果(useIPv4){
如果(isIPv4)
返回SADDR;
}其他{
如果(!isIPv4){
//下降IP6端口后缀
INT DELIM = sAddr.indexOf('%');
返回DELIM< 0? SADDR:sAddr.substring(0,DELIM);
}
}
}
}
}
}赶上(例外前){
ex.printStackTrace();
}
返回;
}
这是最简便的方法。
希望我的回答是有帮助的。
Is there any Simple Way to get the IP address Of my phone when connected to internet through mobile data Network. For getting WiFi IP address i am using following simple Technique.
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
Is there any way similar to above to get IP address of mobile data network.
I have used following code but it returns MAC addresses ,IP addresses of both WiFi and cellular network but i am interested only in Cellular IP Address.
String ipAddress = null;
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()) {
ipAddress = inetAddress.getHostAddress().toString();
Log.i("Sarao5",ipAddress);
}
}
}
} catch (SocketException ex) {}
Use below code as i use in my app -
public static String getDeviceIPAddress(boolean useIPv4) {
try {
List<NetworkInterface> networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface networkInterface : networkInterfaces) {
List<InetAddress> inetAddresses = Collections.list(networkInterface.getInetAddresses());
for (InetAddress inetAddress : inetAddresses) {
if (!inetAddress.isLoopbackAddress()) {
String sAddr = inetAddress.getHostAddress().toUpperCase();
boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
if (useIPv4) {
if (isIPv4)
return sAddr;
} else {
if (!isIPv4) {
// drop ip6 port suffix
int delim = sAddr.indexOf('%');
return delim < 0 ? sAddr : sAddr.substring(0, delim);
}
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
this is best and easy way.
Hope my answer is helpfull.
这篇关于Android的获取IP地址时,连接到蜂窝网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!