Android 판단 네트워크 상태 실례 상세 설명

Android 판단 네트워크 상태 실례 상세 설명
인 스 턴 스 코드

package com.example.android; 
 
import java.io.IOException; 
import java.net.HttpURLConnection; 
import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.SocketException; 
import java.net.URL; 
import java.util.Enumeration; 
 
import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.telephony.TelephonyManager; 
 
public class NetStatus { 
 
  public static int NET_CNNT_BAIDU_OK = 1; //           
  public static int NET_CNNT_BAIDU_TIMEOUT = 2; //           
  public static int NET_NOT_PREPARE = 3; //        
  public static int NET_ERROR = 4; 
  private static int TIMEOUT = 3000; 
 
  /** 
   *          
   * 
   * @param context 
   * @return 
   */ 
  public static int getNetState(Context context) { 
  try { 
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    if (connectivity != null) { 
    NetworkInfo networkinfo = connectivity.getActiveNetworkInfo(); 
    if (networkinfo != null) { 
      if (networkinfo.isAvailable() && networkinfo.isConnected()) { 
      if (!connectionNetwork()) 
        return NET_CNNT_BAIDU_TIMEOUT; 
      else 
        return NET_CNNT_BAIDU_OK; 
      } else { 
      return NET_NOT_PREPARE; 
      } 
    } 
    } 
  } catch (Exception e) { 
  } 
  return NET_ERROR; 
  } 
 
  /** 
   *       
   * 
   * @return 
   */ 
  private static boolean connectionNetwork() { 
  boolean result = false; 
  HttpURLConnection httpUrl = null; 
  try { 
    httpUrl = (HttpURLConnection) new URL("http://www.baidu.com").openConnection(); 
    httpUrl.setConnectTimeout(TIMEOUT); 
    httpUrl.connect(); 
    result = true; 
  } catch (IOException e) { 
  } finally { 
    if (null != httpUrl) { 
    httpUrl.disconnect(); 
    } 
    httpUrl = null; 
  } 
  return result; 
  } 
 
  /** 
   *          3G   
   * 
   * @param context 
   * @return boolean 
   */ 
  public static boolean is3G(Context context) { 
  ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
  NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 
  if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) { 
    return true; 
  } 
  return false; 
  } 
 
  /** 
   *          wifi   
   * 
   * @param context 
   * @return boolean 
   */ 
  public static boolean isWifi(Context context) { 
  ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
  NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 
  if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { 
    return true; 
  } 
  return false; 
  } 
 
  /** 
   *          2G   
   * 
   * @param context 
   * @return boolean 
   */ 
  public static boolean is2G(Context context) { 
  ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
  NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 
  if (activeNetInfo != null && (activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE 
      || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS  
      || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA)) { 
    return true; 
  } 
  return false; 
  } 
 
  /** 
   * wifi     
   */ 
  public static boolean isWifiEnabled(Context context) { 
  ConnectivityManager mgrConn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
  TelephonyManager mgrTel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
  return ((mgrConn.getActiveNetworkInfo() != null && mgrConn.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED)  
    || mgrTel.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS); 
  } 
 
  /** 
   *     ip   
   * 
   * @return 
   */ 
  public static String GetHostIp() { 
  try { 
    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { 
    NetworkInterface intf = en.nextElement(); 
    for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr.hasMoreElements();) { 
      InetAddress inetAddress = ipAddr.nextElement(); 
      if (!inetAddress.isLoopbackAddress()) { 
      return inetAddress.getHostAddress(); 
      } 
    } 
    } 
  } catch (SocketException ex) { 
  } catch (Exception e) { 
  } 
  return null; 
  } 
 
  /** 
   *       imei 
   * 
   * @param context 
   * @return 
   */ 
  public static String getIMEI(Context context) { 
  TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
  return telephonyManager.getDeviceId(); 
  } 
} 

추가 권한:네트워크 접근 권한

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
<uses-permission android:name="android.permission.INTERNET"/>  
읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기