Android系统可以通过WIFI和移动网络GPRS或者3G上网,使用不同网络上网的时候本机的IP地址并不一样。这里予以总结Android系统获取IP地址的方法和获取MAC地址的方法。

1、使用WIFI时,获取本机IP地址

很明显使用WIFI的时候,要想获取本机的IP地址是通过WIFI服务(WIFI_SERVICE)来获取的。

  获取WIFI服务方法:




[java]  view plain  copy



  1. WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);   



  获取IP地址方法:



[java]  view plain  copy



1. <span style="white-space:pre">  </span>/***
2.      * 使用WIFI时,获取本机IP地址
3.      * @param mContext
4.      * @return
5.      */  
6. public static String getWIFILocalIpAdress(Context mContext) {  
7.         
8. //获取wifi服务  
9.         WifiManager wifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);  
10. //判断wifi是否开启  
11. if (!wifiManager.isWifiEnabled()) {  
12. true);   
13.         }  
14.         WifiInfo wifiInfo = wifiManager.getConnectionInfo();      
15. int ipAddress = wifiInfo.getIpAddress();  
16.         String ip = formatIpAddress(ipAddress);  
17. return ip;  
18.     }    
19. private static String formatIpAddress(int ipAdress) {      
20.         
21. return (ipAdress & 0xFF ) + "." +      
22. 8 ) & 0xFF) + "." +      
23. 16 ) & 0xFF) + "." +      
24. 24 & 0xFF) ;  
25.      }

这里将IP地址格式化0单独作为一个方法。


说明:如果要在应用中使用需要添加WIFI访问的相关权限:



[html]  view plain  copy


1. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>   
2. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>   
3. <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>


2、获取本机MAC地址



[java]  view plain  copy


1. /**
2.  * 获取MAC地址
3.  * @param mContext
4.  * @return
5.  */  
6. public static String getMacAddress(Context mContext) {  
7. "";  
8.     WifiManager wifiManager = (WifiManager) mContext  
9.             .getSystemService(Context.WIFI_SERVICE);  
10.     WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
11. if (wifiInfo.getMacAddress() != null) {  
12. // MAC地址  
13. else {  
14. "null";  
15.     }  
16.   
17. return macStr;  
18. }


说明:获取本机MAC地址之前,需要通过WIFI连接一下网络(需要初始化硬件),才能获取到本机的MAC地址。


3、使用GPRS时,获取本机IP地址



[java]  view plain  copy



    1. <span style="white-space:pre">  </span>/**
    2.      * 使用GPRS时,获取本机IP地址
    3.      * @return
    4.      */  
    5. public static String getGPRSLocalIpAddress() {  
    6. try {  
    7. for (Enumeration<NetworkInterface> en = NetworkInterface  
    8.                     .getNetworkInterfaces(); en.hasMoreElements();) {  
    9.                 NetworkInterface intf = en.nextElement();  
    10. for (Enumeration<InetAddress> enumIpAddr = intf  
    11.                         .getInetAddresses(); enumIpAddr.hasMoreElements();) {  
    12.                     InetAddress inetAddress = enumIpAddr.nextElement();  
    13. if (!inetAddress.isLoopbackAddress()) {  
    14. return inetAddress.getHostAddress().toString();  
    15.                     }  
    16.                 }  
    17.             }  
    18. catch (SocketException ex) {  
    19. "WifiPreference IpAddress", ex.toString());  
    20.         }  
    21. return null;  
    22.     }


    说明:如果在应用中使用时需要添加权限:



    [html]  view plain  copy



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




    4、获取网关的IP地址

    这个无论是WIFI上网或者GPRS上网,均可使用



    [java]  view plain  copy



      1. /***
      2.  * 获取网关IP地址
      3.  * @return
      4.  */  
      5. public static String getHostIp() {  
      6. try {  
      7. for (Enumeration<NetworkInterface> en = NetworkInterface  
      8.                 .getNetworkInterfaces(); en.hasMoreElements();) {  
      9.             NetworkInterface intf = en.nextElement();  
      10. for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr  
      11.                     .hasMoreElements();) {  
      12.                 InetAddress inetAddress = ipAddr.nextElement();  
      13. if (!inetAddress.isLoopbackAddress()) {  
      14. return inetAddress.getHostAddress();  
      15.                 }  
      16.             }  
      17.         }  
      18. catch (SocketException ex) {  
      19. catch (Exception e) {  
      20.     }  
      21. return null;  
      22. }

      Android系统可以通过WIFI和移动网络GPRS或者3G上网,使用不同网络上网的时候本机的IP地址并不一样。这里予以总结Android系统获取IP地址的方法和获取MAC地址的方法。

      1、使用WIFI时,获取本机IP地址

      很明显使用WIFI的时候,要想获取本机的IP地址是通过WIFI服务(WIFI_SERVICE)来获取的。

        获取WIFI服务方法:




      [java]  view plain  copy



      1. WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);   



        获取IP地址方法:



      [java]  view plain  copy


      1. <span style="white-space:pre">  </span>/***
      2.      * 使用WIFI时,获取本机IP地址
      3.      * @param mContext
      4.      * @return
      5.      */  
      6. public static String getWIFILocalIpAdress(Context mContext) {  
      7.         
      8. //获取wifi服务  
      9.         WifiManager wifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);  
      10. //判断wifi是否开启  
      11. if (!wifiManager.isWifiEnabled()) {  
      12. true);   
      13.         }  
      14.         WifiInfo wifiInfo = wifiManager.getConnectionInfo();      
      15. int ipAddress = wifiInfo.getIpAddress();  
      16.         String ip = formatIpAddress(ipAddress);  
      17. return ip;  
      18.     }    
      19. private static String formatIpAddress(int ipAdress) {      
      20.         
      21. return (ipAdress & 0xFF ) + "." +      
      22. 8 ) & 0xFF) + "." +      
      23. 16 ) & 0xFF) + "." +      
      24. 24 & 0xFF) ;  
      25.      }

      这里将IP地址格式化0单独作为一个方法。


      说明:如果要在应用中使用需要添加WIFI访问的相关权限:



      [html]  view plain  copy

      1. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>   
      2. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>   
      3. <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>


      2、获取本机MAC地址



      [java]  view plain  copy


      1. /**
      2.  * 获取MAC地址
      3.  * @param mContext
      4.  * @return
      5.  */  
      6. public static String getMacAddress(Context mContext) {  
      7. "";  
      8.     WifiManager wifiManager = (WifiManager) mContext  
      9.             .getSystemService(Context.WIFI_SERVICE);  
      10.     WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
      11. if (wifiInfo.getMacAddress() != null) {  
      12. // MAC地址  
      13. else {  
      14. "null";  
      15.     }  
      16.   
      17. return macStr;  
      18. }



      说明:获取本机MAC地址之前,需要通过WIFI连接一下网络(需要初始化硬件),才能获取到本机的MAC地址。


      3、使用GPRS时,获取本机IP地址



      [java]  view plain  copy


      1. <span style="white-space:pre">  </span>/**
      2.      * 使用GPRS时,获取本机IP地址
      3.      * @return
      4.      */  
      5. public static String getGPRSLocalIpAddress() {  
      6. try {  
      7. for (Enumeration<NetworkInterface> en = NetworkInterface  
      8.                     .getNetworkInterfaces(); en.hasMoreElements();) {  
      9.                 NetworkInterface intf = en.nextElement();  
      10. for (Enumeration<InetAddress> enumIpAddr = intf  
      11.                         .getInetAddresses(); enumIpAddr.hasMoreElements();) {  
      12.                     InetAddress inetAddress = enumIpAddr.nextElement();  
      13. if (!inetAddress.isLoopbackAddress()) {  
      14. return inetAddress.getHostAddress().toString();  
      15.                     }  
      16.                 }  
      17.             }  
      18. catch (SocketException ex) {  
      19. "WifiPreference IpAddress", ex.toString());  
      20.         }  
      21. return null;  
      22.     }

       



      说明:如果在应用中使用时需要添加权限:



      [html]  view plain  copy



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




      4、获取网关的IP地址

      这个无论是WIFI上网或者GPRS上网,均可使用



      [java]  view plain  copy


      1. /***
      2.  * 获取网关IP地址
      3.  * @return
      4.  */  
      5. public static String getHostIp() {  
      6. try {  
      7. for (Enumeration<NetworkInterface> en = NetworkInterface  
      8.                 .getNetworkInterfaces(); en.hasMoreElements();) {  
      9.             NetworkInterface intf = en.nextElement();  
      10. for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr  
      11.                     .hasMoreElements();) {  
      12.                 InetAddress inetAddress = ipAddr.nextElement();  
      13. if (!inetAddress.isLoopbackAddress()) {  
      14. return inetAddress.getHostAddress();  
      15.                 }  
      16.             }  
      17.         }  
      18. catch (SocketException ex) {  
      19. catch (Exception e) {  
      20.     }  
      21. return null;  
      22. }