广告
返回顶部
首页 > 资讯 > 精选 >Android应用中是如何判断网络状态的
  • 215
分享到

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

androidroi 2023-05-31 06:05:12 215人浏览 泡泡鱼
摘要

Android应用中是如何判断网络状态的?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。实例代码package com.example.android; import ja

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;       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;   }       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;   }       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;   }       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;   }       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;   }       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);   }       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;   }       public static String getIMEI(Context context) {   TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);   return telephonyManager.getDeviceId();   } } 

--结束END--

本文标题: Android应用中是如何判断网络状态的

本文链接: https://www.lsjlt.com/news/223961.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作