判断当前网络是否可用

年夜雪

关注

阅读 61

2023-03-31

 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)

0 0 举报