天天看点

Android获取本机IP地址(不是localhost)和MAC的方法

这个方法在摩托罗拉里程碑上测试通过。功能是获取本机的IP和MAC地址。首先新建一个工程,修改AndroidManifest.xml文件增加用户权限,如下。

view plain

  1. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>  
  2. <uses-permission android:name="android.permission.INTERNET"></uses-permission>  

      然后修改/res/layout/main.xml,如下。

view plain

  1. <TextView    
  2.     android:id ="@+id/hello"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="wrap_content"  
  5.     />  

      主要代码如下(GetIPMAC.java):

view plain

  1. package exp.getipmac;  
  2. import java.net.InetAddress;  
  3. import java.net.NetworkInterface;  
  4. import java.net.SocketException;  
  5. import java.util.Enumeration;  
  6. import android.app.Activity;  
  7. import android.content.Context;  
  8. import android.net.wifi.WifiInfo;  
  9. import android.net.wifi.WifiManager;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.widget.TextView;  
  13. public class GetIPMAC extends Activity {  
  14.     public static String hostip;             //本机IP  
  15.     public static String hostmac;            //本机MAC  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         TextView tv= (TextView)findViewById(R.id.hello);  
  21.         hostip = getLocalIpAddress();  //获取本机IP  
  22.         hostmac = getLocalMacAddress();//获取本机MAC  
  23.         tv.setText("HostIP:" + hostip + "/nHostMAC:" + hostmac);  
  24.         if (hostip != null) Log.d("GetIPMAC", hostip);  
  25.         else Log.d("GetIPMAC", "null");  
  26.         Log.d("GetIPMAC", hostmac);  
  27.     }  
  28.     public String getLocalIpAddress() {  
  29.         try {  
  30.             for (Enumeration<NetworkInterface> en = NetworkInterface  
  31.                     .getNetworkInterfaces(); en.hasMoreElements();) {  
  32.                 NetworkInterface intf = en.nextElement();  
  33.                 for (Enumeration<InetAddress> enumIpAddr = intf  
  34.                         .getInetAddresses(); enumIpAddr.hasMoreElements();) {  
  35.                     InetAddress inetAddress = enumIpAddr.nextElement();  
  36.                     if (!inetAddress.isLoopbackAddress()) {  
  37.                         return inetAddress.getHostAddress().toString();  
  38.                     }  
  39.                 }  
  40.             }  
  41.         } catch (SocketException ex) {  
  42.             Log.e("WifiPreference IpAddress", ex.toString());  
  43.         }  
  44.         return null;  
  45.     }  
  46.     public String getLocalMacAddress() {  
  47.         WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  48.         WifiInfo info = wifi.getConnectionInfo();  
  49.         return info.getMacAddress();  
  50.     }  
  51. }  

      运行效果:

Android获取本机IP地址(不是localhost)和MAC的方法