My phone has an IP. I would like to know how could I retrieve it? After searching in the web, I found that i c开发者_运维技巧an get only the current outside ip.(I want the local-perm ip of my phone)
Thanks,
ray.
A quick search on google sent me here: http://www.droidnova.com/get-the-ip-address-of-your-device,304.html
Read the comments about how to use the first block of code to get the wifi ip address (on local network, not public ip)
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
Edit: The emulator seems to return 0 on wifiInfo.getIpAddress(), but it works fine on a phone. The following code converts the integer to an ip address:
String ipBinary = Integer.toBinaryString(ipAddress);
//Leading zeroes are removed by toBinaryString, this will add them back.
while(ipBinary.length() < 32) {
ipBinary = "0" + ipBinary;
}
//get the four different parts
String a=ipBinary.substring(0,8);
String b=ipBinary.substring(8,16);
String c=ipBinary.substring(16,24);
String d=ipBinary.substring(24,32);
//Convert to numbers
String actualIpAddress =Integer.parseInt(d,2)+"."+Integer.parseInt(c,2)+"."+Integer.parseInt(b,2)+"."+Integer.parseInt(a,2);
your ip will change with every networkthat you connect to - your phone has a mac address - is that what you are looking to find?
The IP which I may would call "local-perm" is the localhost
IP which is always 127.0.0.1
The IP of any other network adapter (Wifi in this case) changes depending on the network
精彩评论