Android 獲取網絡狀態的工具類

在工作中我們經常要用到網絡,網絡狀態的檢查必不可少,這是一個檢查當前網絡狀態的工具類,直接粘過去就能用:

[java]
package com.todoo.android.app.utils; 
 
import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.SocketException; 
import java.util.Enumeration; 
 
import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
 
 
public class NetworkUtils 

    public static final String NET_TYPE_WIFI = "WIFI"; 
    public static final String NET_TYPE_MOBILE = "MOBILE"; 
    public static final String NET_TYPE_NO_NETWORK = "no_network"; 
     
    private Context mContext = null; 
     
    public NetworkUtils(Context pContext) { 
        this.mContext = pContext; 
    } 
     
    public static final String IP_DEFAULT = "0.0.0.0"; 
 
    public static boolean isConnectInternet(final Context pContext) 
    { 
        final ConnectivityManager conManager = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE); 
        final NetworkInfo networkInfo = conManager.getActiveNetworkInfo(); 
 
        if (networkInfo != null) 
        { 
            return networkInfo.isAvailable(); 
        } 
 
        return false; 
    } 
     
    public static boolean isConnectWifi(final Context pContext) { 
        ConnectivityManager mConnectivity = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo info = mConnectivity.getActiveNetworkInfo(); 
        //判斷網絡連接類型,隻有在3G或wifi裡進行一些數據更新。    
        int netType = -1; 
        if(info != null){ 
            netType = info.getType(); 
        } 
        if (netType == ConnectivityManager.TYPE_WIFI) { 
            return info.isConnected(); 
        } else { 
            return false; 
        } 
    } 
 
    public static String getNetTypeName(final int pNetType) 
    { 
        switch (pNetType) 
        { 
            case 0: 
                return "unknown"; 
            case 1: 
                return "GPRS"; 
            case 2: 
                return "EDGE"; 
            case 3: 
                return "UMTS"; 
            case 4: 
                return "CDMA: Either IS95A or IS95B"; 
            case 5: 
                return "EVDO revision 0"; 
            case 6: 
                return "EVDO revision A"; 
            case 7: 
                return "1xRTT"; 
            case 8: 
                return "HSDPA"; 
            case 9: 
                return "HSUPA"; 
            case 10: 
                return "HSPA"; 
            case 11: 
                return "iDen"; 
            case 12: 
                return "EVDO revision B"; 
            case 13: 
                return "LTE"; 
            case 14: 
                return "eHRPD"; 
            case 15: 
                return "HSPA+"; 
            default: 
                return "unknown"; 
        } 
    } 
 
    public static String getIPAddress() 
    { 
        try 
        { 
            final Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces(); 
 
            while (networkInterfaceEnumeration.hasMoreElements()) 
            { 
                final NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement(); 
 
                final Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses(); 
 
                while (inetAddressEnumeration.hasMoreElements()) 
                { 
                    final InetAddress inetAddress = inetAddressEnumeration.nextElement(); 
 
                    if (!inetAddress.isLoopbackAddress()) 
                    { 
                        return inetAddress.getHostAddress(); 
                    } 
                } 
            } 
 
            return NetworkUtils.IP_DEFAULT; 
        } 
        catch (final SocketException e) 
        { 
            return NetworkUtils.IP_DEFAULT; 
        } 
    } 
     
    public String getConnTypeName() { 
        ConnectivityManager connectivityManager = (ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 
        if(networkInfo == null) { 
            return NET_TYPE_NO_NETWORK; 
        } else { 
            return networkInfo.getTypeName(); 
        } 
    } 

package com.todoo.android.app.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtils
{
 public static final String NET_TYPE_WIFI = "WIFI";
 public static final String NET_TYPE_MOBILE = "MOBILE";
 public static final String NET_TYPE_NO_NETWORK = "no_network";
 
 private Context mContext = null;
 
 public NetworkUtils(Context pContext) {
  this.mContext = pContext;
 }
 
 public static final String IP_DEFAULT = "0.0.0.0";

 public static boolean isConnectInternet(final Context pContext)
 {
  final ConnectivityManager conManager = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
  final NetworkInfo networkInfo = conManager.getActiveNetworkInfo();

  if (networkInfo != null)
  {
   return networkInfo.isAvailable();
  }

  return false;
 }
 
 public static boolean isConnectWifi(final Context pContext) {
  ConnectivityManager mConnectivity = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo info = mConnectivity.getActiveNetworkInfo();
  //判斷網絡連接類型,隻有在3G或wifi裡進行一些數據更新。 
  int netType = -1;
  if(info != null){
   netType = info.getType();
  }
  if (netType == ConnectivityManager.TYPE_WIFI) {
   return info.isConnected();
  } else {
   return false;
  }
 }

 public static String getNetTypeName(final int pNetType)
 {
  switch (pNetType)
  {
   case 0:
    return "unknown";
   case 1:
    return "GPRS";
   case 2:
    return "EDGE";
   case 3:
    return "UMTS";
   case 4:
    return "CDMA: Either IS95A or IS95B";
   case 5:
    return "EVDO revision 0";
   case 6:
    return "EVDO revision A";
   case 7:
    return "1xRTT";
   case 8:
    return "HSDPA";
   case 9:
    return "HSUPA";
   case 10:
    return "HSPA";
   case 11:
    return "iDen";
   case 12:
    return "EVDO revision B";
   case 13:
    return "LTE";
   case 14:
    return "eHRPD";
   case 15:
    return "HSPA+";
   default:
    return "unknown";
  }
 }

 public static String getIPAddress()
 {
  try
  {
   final Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();

   while (networkInterfaceEnumeration.hasMoreElements())
   {
    final NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();

    final Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();

    while (inetAddressEnumeration.hasMoreElements())
    {
     final InetAddress inetAddress = inetAddressEnumeration.nextElement();

     if (!inetAddress.isLoopbackAddress())
     {
      return inetAddress.getHostAddress();
     }
    }
   }

   return NetworkUtils.IP_DEFAULT;
  }
  catch (final SocketException e)
  {
   return NetworkUtils.IP_DEFAULT;
  }
 }
 
 public String getConnTypeName() {
  ConnectivityManager connectivityManager = (ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  if(networkInfo == null) {
   return NET_TYPE_NO_NETWORK;
  } else {
   return networkInfo.getTypeName();
  }
 }
}最後不要忘瞭加上權限:

<!– 獲取網絡狀態的權限 –>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。