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

这个方法在摩托罗拉里程碑上测试通过。功能是获取本机的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. }   


      运行效果:

(图)Android获取本机IP地址(不是localhost)和MAC的方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值