android检测当前网络是否可用

package cn.chen.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkDetector {
 /**
  * detect the current network
  *
  * @param context
  * @return
  */
 public static boolean detect(Context context) {

  ConnectivityManager manager = (ConnectivityManager) context
    .getApplicationContext().getSystemService(
      Context.CONNECTIVITY_SERVICE);

  if (manager == null) {
   return false;
  }

  NetworkInfo networkinfo = manager.getActiveNetworkInfo();

  if (networkinfo == null || !networkinfo.isAvailable()) {
   return false;
  }

  return true;
 }

}

你可能感兴趣的:(android检测当前网络是否可用)