您的位置:首页 > 运维架构 > Linux

Linux下的Android电话管理软件——QtADB(安装启动方法)

2011-09-03 10:23 966 查看
在wifi环境下,可以通过WifiInfo来获取设备的ip
public String getIpAddress() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
int[] ipAddr = new int[4];
ipAddr[0] = ipAddress & 0xFF;
ipAddr[1] = (ipAddress >> 8) & 0xFF;
ipAddr[2] = (ipAddress >> 16) & 0xFF;
ipAddr[3] = (ipAddress >> 24) & 0xFF;
return new StringBuilder().append(ipAddr[0]).append(".").append(ipAddr[1]).append(".").append(ipAddr[2])
.append(".").append(ipAddr[3]).append(".").toString();

}


执行上面的代码需要
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />


当然也可通过jdk总的NetworkInterface来获取的,就是遍历所有的网络接口,获取到非loopback ip
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 (SocketException ex) {
Log.e("", ex.toString());
}
return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: