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

来源:互联网 发布:网页版windows系统 编辑:程序博客网 时间:2024/05/18 01:00

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

分类: Android 5822人阅读 评论(7) 收藏 举报
androidstringlayoutnullserviceclass

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

      

[xhtml] view plaincopy
  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,如下。

      

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

 

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

      

[java] view plaincopy
  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.       
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         TextView tv= (TextView)findViewById(R.id.hello);  
  23.           
  24.         hostip = getLocalIpAddress();  //获取本机IP  
  25.         hostmac = getLocalMacAddress();//获取本机MAC  
  26.         /* 显示本机IP和MAC */  
  27.         tv.setText("HostIP:" + hostip + "/nHostMAC:" + hostmac);  
  28.         /* 在调试信息中输出本机IP和MAC */  
  29.         if (hostip != null) Log.d("GetIPMAC", hostip);  
  30.         else Log.d("GetIPMAC""null");  
  31.         Log.d("GetIPMAC", hostmac);  
  32.     }  
  33.       
  34.     public String getLocalIpAddress() {  
  35.         try {  
  36.             for (Enumeration<NetworkInterface> en = NetworkInterface  
  37.                     .getNetworkInterfaces(); en.hasMoreElements();) {  
  38.                 NetworkInterface intf = en.nextElement();  
  39.                 for (Enumeration<InetAddress> enumIpAddr = intf  
  40.                         .getInetAddresses(); enumIpAddr.hasMoreElements();) {  
  41.                     InetAddress inetAddress = enumIpAddr.nextElement();  
  42.                     if (!inetAddress.isLoopbackAddress()) {  
  43.                         return inetAddress.getHostAddress().toString();  
  44.                     }  
  45.                 }  
  46.             }  
  47.         } catch (SocketException ex) {  
  48.             Log.e("WifiPreference IpAddress", ex.toString());  
  49.         }  
  50.         return null;  
  51.     }  
  52.       
  53.     public String getLocalMacAddress() {  
  54.         WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  55.         WifiInfo info = wifi.getConnectionInfo();  
  56.         return info.getMacAddress();  
  57.     }  
  58. }  

 

      运行效果:

原创粉丝点击