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

来源:互联网 发布:苹果6为什么打不开淘宝 编辑:程序博客网 时间:2024/06/07 05:25

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

      
view plaincopy to clipboardprint?
<uses-permission Android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>  
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

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

      

XML/HTML代码
  1. view plaincopy to clipboardprint?   
  2. <TextView       
  3.     android:id ="@+id/hello"     
  4.     android:layout_width="fill_parent"      
  5.     android:layout_height="wrap_content"     
  6.     />    

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

      

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


      运行效果: