Android获取本机Mac地址和IP地址
1、获取Mac地址:
在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
获取:
public String getLocalMacAddress() { WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress(); }
2、获取IP地址:
在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.INTERNET"/>
获取:
public String getLocalIpAddress() { try { String ipv4; List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface ni: nilist){ List<InetAddress> ialist = Collections.list(ni.getInetAddresses()); for (InetAddress address: ialist){ if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress())){ return ipv4; } } } } catch (SocketException ex) { Log.e("exception", ex.toString()); } return null; }
相关推荐
Micusd 2020-11-19
cheidou 2020-11-19
星马殇 2020-11-18
Echodat 2020-10-08
flyToSkyL 2020-09-11
zjc 2020-09-03
阳光普照 2020-09-01
十年砍柴 2020-08-30
猪猪侠喜欢躲猫猫 2020-08-17
ladewang 2020-08-17
渣渣灰 2020-08-13
blankt 2020-08-15
vickay 2020-08-09
TinyDolphin 2020-08-09
山兔与孟婆 2020-08-09
longjing 2020-08-07
xiaogoua 2020-08-06