android獲得mac和ip .
1、Android 獲取本機Mac 地址方法:
需要在AndroidManifest.xml文件中添加權限:
- <span style="font-family:Arial;BACKGROUND-COLOR: #ffffff"> </span><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;
- }
最後更新:2017-04-02 06:51:53