Kje*_*dal 10
谷歌快速搜索发送给我:http: //www.droidnova.com/get-the-ip-address-of-your-device,304.html
阅读有关如何使用第一个代码块获取wifi IP地址的评论(在本地网络上,而不是公共IP)
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
Run Code Online (Sandbox Code Playgroud)
编辑:模拟器似乎在wifiInfo.getIpAddress()上返回0,但它在手机上工作正常.以下代码将整数转换为ip地址:
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4189 次 |
| 最近记录: |