Android 擷取網路狀態的工具類

來源:互聯網
上載者:User

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


[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" />


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.