首页 > 代码库 > Android-判断当前网络是否可用
Android-判断当前网络是否可用
1.声明权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2.网络检测类
public class NetworkDetector { /** Network type is unknown */ public static final int NETWORK_TYPE_UNKNOWN = 0; /** Current network is GPRS */ public static final int NETWORK_TYPE_GPRS = 1; /** Current network is EDGE */ public static final int NETWORK_TYPE_EDGE = 2; /** Current network is UMTS */ public static final int NETWORK_TYPE_UMTS = 3; /** Current network is CDMA: Either IS95A or IS95B */ public static final int NETWORK_TYPE_CDMA = 4; /** Current network is EVDO revision 0 */ public static final int NETWORK_TYPE_EVDO_0 = 5; /** Current network is EVDO revision A */ public static final int NETWORK_TYPE_EVDO_A = 6; /** Current network is 1xRTT */ public static final int NETWORK_TYPE_1xRTT = 7; /** Current network is HSDPA */ public static final int NETWORK_TYPE_HSDPA = 8; /** Current network is HSUPA */ public static final int NETWORK_TYPE_HSUPA = 9; /** Current network is HSPA */ public static final int NETWORK_TYPE_HSPA = 10; /** Current network is iDen */ public static final int NETWORK_TYPE_IDEN = 11; /** Current network is EVDO revision B */ public static final int NETWORK_TYPE_EVDO_B = 12; /** Current network is LTE */ public static final int NETWORK_TYPE_LTE = 13; /** Current network is eHRPD */ public static final int NETWORK_TYPE_EHRPD = 14; /** Current network is HSPA+ */ public static final int NETWORK_TYPE_HSPAP = 15; /** Unknown network class. {@hide} */ public static final int NETWORK_CLASS_UNKNOWN = 0; /** Class of broadly defined "2G" networks. {@hide} */ public static final int NETWORK_CLASS_2_G = 1; /** Class of broadly defined "3G" networks. {@hide} */ public static final int NETWORK_CLASS_3_G = 2; /** Class of broadly defined "4G" networks. {@hide} */ public static final int NETWORK_CLASS_4_G = 3; /** Class of broadly defined "WIFI" networks. {@hide} */ public static final int NETWORK_CLASS_WIFI = 4; /** Class of broadly defined "NONET" networks. {@hide} */ public static final int NETWORK_CLASS_NONET = 5; /** * 判断当前网络是否可用 * * @param context * @return true:可用,false:不可用; */ public static boolean isAvailable(Context context) { ConnectivityManager manager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (manager == null) { return false; } NetworkInfo networkinfo = manager.getActiveNetworkInfo(); // NetworkInfo.isAvailable() 如果网络可用返回true,否则返回false。 if (networkinfo == null || !networkinfo.isAvailable()) { return false; } else { return true; } } /** * 判断当前的网络状态 * * @param context * @return 0、未知, 1、2G, 2、3G, 3、4G, 4、wifi, 5、noNet; */ public static int getNetWorkType(Context context) { ConnectivityManager manager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (manager == null) { return NETWORK_CLASS_NONET; } NetworkInfo networkinfo = manager.getActiveNetworkInfo(); if (networkinfo == null || !networkinfo.isAvailable()) { return NETWORK_CLASS_NONET; } if (networkinfo.getType() == ConnectivityManager.TYPE_WIFI) { return NETWORK_CLASS_WIFI; } else { return getNetworkClass(((TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE)) .getNetworkType()); } } public static int getNetworkClass(int networkType) { switch (networkType) { case NETWORK_TYPE_GPRS: case NETWORK_TYPE_EDGE: case NETWORK_TYPE_CDMA: case NETWORK_TYPE_1xRTT: case NETWORK_TYPE_IDEN: return NETWORK_CLASS_2_G; case NETWORK_TYPE_UMTS: case NETWORK_TYPE_EVDO_0: case NETWORK_TYPE_EVDO_A: case NETWORK_TYPE_HSDPA: case NETWORK_TYPE_HSUPA: case NETWORK_TYPE_HSPA: case NETWORK_TYPE_EVDO_B: case NETWORK_TYPE_EHRPD: case NETWORK_TYPE_HSPAP: return NETWORK_CLASS_3_G; case NETWORK_TYPE_LTE: return NETWORK_CLASS_4_G; default: return NETWORK_CLASS_UNKNOWN; } }}
Android-判断当前网络是否可用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。