Android 检查网络状态是否可用 (工具类总结三)
16lz
2021-01-23
public class NetCheckUtil { public static boolean checkNet(Context context) { // 判断是否具有可以用于通信渠道 boolean mobileConnection = isMobileConnection(context); boolean wifiConnection = isWIFIConnection(context); if ( mobileConnection == false && wifiConnection == false ) { // 没有网络 return false; } return true; } /** * 判断手机接入点(APN)是否处于可以使用的状态 * * @param context * @return */ public static boolean isMobileConnection(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if ( networkInfo != null && networkInfo.isConnected() ) { return true; } return false; } /** * 判断当前wifi是否是处于可以使用状态 * * @param context * @return */ public static boolean isWIFIConnection(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if ( networkInfo != null && networkInfo.isConnected() ) { return true; } return false; }}
直接调用
if(NetCheckUtil.checkNet(context)){//网络可用}else{//网络不可用}
更多相关文章
- Android网络服务开发之http编程
- Android 获取网络图片方法
- [android]简单方法记载 在adapter中异步加载网络图片
- Android-MediaPlayer播放网络音频
- [置顶] Android相关网络资源整理
- android从网络获取图片
- android使用volley等网络资源请求时注意事项(*)
- android之线程访问网络并解析数据
- Android中判断网络连接是否可用的方法总结