在清单文件中加入以下权限AndroidManifest.xml:
引用
<!-- 访问网络 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 访问Wi-Fi网络 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />


获取Android本机IP地址和MAC,Test.java类:
package com.iaiai.test;import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.util.Enumeration;import android.content.Context;import android.net.wifi.WifiInfo;import android.net.wifi.WifiManager;import android.test.AndroidTestCase;import android.util.Log;public class Test extends AndroidTestCase {private final String TAG = "Test";public void testLocalIpAndMac() {Log.i(TAG, "IP: " + getLocalIpAddress() + ", MAC: "+ getLocalMacAddress());}/** * 获取Android本机IP地址 *  * @return */private 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 *  * @return */private String getLocalMacAddress() {WifiManager wifi = (WifiManager) this.getContext().getSystemService(Context.WIFI_SERVICE);WifiInfo info = wifi.getConnectionInfo();return info.getMacAddress();}}


运行结果:

更多相关文章

  1. android判断当前网络状态,eth wifi pppoe等
  2. Android获取当前网络状态和获取当前设备网络ip地址
  3. network: android 网络判断
  4. android资料下载地址汇总
  5. android网络优化

随机推荐

  1. android ListView根据字母排序和定位
  2. Android(安卓)应用程序之间数据共享—Con
  3. 系出名门Android(2) - 布局(Layout)和菜
  4. android webview自定义标签!(实现打电话的
  5. 通过修改hosts文件成功更新Android(安卓)
  6. android媒体--stagefright概述【一】
  7. android 登陆、提交数据或加载数据时提示
  8. Android(安卓)-- 多线程下载
  9. Android的SQLite使用介绍
  10. Android(安卓)Gradle编译学习日记之二(使