0
点赞
收藏
分享

微信扫一扫

判断当前网络是否可用

年夜雪 2023-03-31 阅读 60

 public static boolean isNetworkAvailable(Context mContext) {

               boolean result = false;

               if (mContext != null) {

                         Context context = mContext.getApplicationContext();

                         ConnectivityManager connectivity = (ConnectivityManager) context

                                               .getSystemService(Context.CONNECTIVITY_SERVICE);

if (connectivity != null) {

                                  NetworkInfo[] info = connectivity.getAllNetworkInfo();

                                  if (info != null) {

                                           for (int i = 0; i < info.length; i++) {

                                                     if (info[i].getState() == NetworkInfo.State.CONNECTED) {

                                                              result = true;

                                                              break;

                                                     }

                                           }

                                  }

                         }

                   }

return result;

    }

举报

相关推荐

0 条评论