android 判断网络连接是否可用

时间:2026-02-17 07:17:21

1、判断连接的工具类

类名:Network_status

android 判断网络连接是否可用

2、/**  * 判断网络连接是否可用  * @param context  * @return  */ public static boolean is_Network_Available(Context context) {           ConnectivityManager cm = (ConnectivityManager) context                   .getSystemService(Context.CONNECTIVITY_SERVICE);           if (cm == null) {           } else {         //如果仅仅是用来判断网络连接         //则可以使用 cm.getActiveNetworkInfo().isAvailable();              NetworkInfo[] info = cm.getAllNetworkInfo();               if (info != null) {                   for (int i = 0; i < info.length; i++) {                       if (info[i].getState() == NetworkInfo.State.CONNECTED) {                           return true;                       }                   }               }           }           return false;       }

android 判断网络连接是否可用

3、 /**  * 判断GPS是否打开  * @param context  * @return  */ public static boolean is_Gps_Enabled(Context context) {           LocationManager lm = ((LocationManager) context                   .getSystemService(Context.LOCATION_SERVICE));           List<String> accessibleProviders = lm.getProviders(true);           return accessibleProviders != null && accessibleProviders.size() > 0;       }

android 判断网络连接是否可用

4、 /**  * 判断WIFI是否打开  * @param context  * @return  */ public static boolean is_Wifi_Enabled(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);       }

android 判断网络连接是否可用

5、 /**  * 判断是否是3G网络  * @param context  * @return  */ public static boolean is_3rd(Context context) {           ConnectivityManager cm = (ConnectivityManager) context                   .getSystemService(Context.CONNECTIVITY_SERVICE);           NetworkInfo networkINfo = cm.getActiveNetworkInfo();           if (networkINfo != null                   && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {               return true;           }           return false;       } 

android 判断网络连接是否可用

6、 /**  * 判断是wifi还是3g网络  * @param context  * @return true(wifi)  */ public static boolean is_Wifi(Context context) {           ConnectivityManager cm = (ConnectivityManager) context                   .getSystemService(Context.CONNECTIVITY_SERVICE);           NetworkInfo networkINfo = cm.getActiveNetworkInfo();           if (networkINfo != null                   && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {               return true;           }           return false;       }

android 判断网络连接是否可用

© 2026 一点资料
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com