Android获取本机Mac地址及IP地址的方法
16lz
2021-12-04
1、Android 获取本机Mac 地址方法:
需要在AndroidManifest.xml文件中添加权限:
2、Android 获取本机IP地址方法:
需要在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、Android 获取本机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("WifiPreference IpAddress", ex.toString());}return null;}
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
- Android(安卓)远程链接 daemon not running 解决方法
- android 获取网络资源
- 问题:在运行android的项目时出现的异常
- android > Android实现计时与倒计时的几种方法
- 屏幕切换,保存内容