检查网络可用与否

public static boolean isNetworkConnected(Context context){
    ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo network = cm.getActiveNetworkInfo();

    if(network != null){
        return network.isAvailable();
    }

    return false;
}

 用这个

private boolean isOnline() {
  ConnectivityManager connec =  (ConnectivityManager)getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
  return connec.getNetworkInfo(0).isConnectedOrConnecting();
 }

 

总是返回false

你可能感兴趣的:(网络)