热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android判断网络状态实例详解

这篇文章主要介绍了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 en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { 
    NetworkInterface intf = en.nextElement(); 
    for (Enumeration 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(); 
  } 
} 

添加权限:访问网络权限

  
  

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


推荐阅读
author-avatar
PHPYeQ
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有