温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android应用中是如何判断网络状态的

发布时间:2020-12-02 17:26:09 来源:亿速云 阅读:124 作者:Leah 栏目:移动开发

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

关于Android应用中是如何判断网络状态的问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI