Android开发中获取本机Mac地址和IP地址的方法
2012-03-13 16:52
801 查看
1、Android 获取本机Mac 地址方法:
需要在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
![](http://blog.csdn.net/images/spinner.gif)
public String getLocalMacAddress() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
return info.getMacAddress();
}
2、Android 获取本机IP地址方法:
![](http://blog.csdn.net/images/spinner.gif)
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("WifiPreference IpAddress", ex.toString());
}
return null;
}
需要在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
![](http://blog.csdn.net/images/spinner.gif)
public String getLocalMacAddress() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
return info.getMacAddress();
}
public String getLocalMacAddress() { WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress(); }
2、Android 获取本机IP地址方法:
![](http://blog.csdn.net/images/spinner.gif)
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("WifiPreference IpAddress", ex.toString());
}
return null;
}
相关文章推荐
- Android获取本机Mac地址及IP地址方法
- android:获取本机Mac地址及IP地址方法
- Android 获取本机Mac 地址方法及ip地址
- android 获取本机的IP地址和mac物理地址的实现方法
- Android获取本机Mac地址及IP地址方法
- Android获取本机IP地址(不是localhost)和MAC的方法
- Android /java获取系统广播地址、ip地址、子网掩码的方法
- jni获取mac地址的问题  -  棒槌网@Android开发论坛 - Powered by phpwind
- Android环境下稳妥获取mac地址的方法
- Android获取本机IP地址(不是localhost)和MAC的方法
- android获取本机的IP地址和mac物理地址
- android 获取设备信息的IP地址和Mac地址—亲测无误!!
- Android下获取以太网mac地址方法 非wifi
- Android获取本机IP地址(不是localhost)和MAC的方法
- Android获取本机IP地址(不是localhost)和MAC的方法
- android获取本机的IP地址和mac物理地址
- python获取本机mac地址和ip地址的方法
- Android获取WIFI状态下的IP地址以及MAC地址
- python获取本机mac地址和ip地址的方法
- Android获取本机IP地址(不是localhost)和MAC的方法