code: public static boolean isConnected(Context context) {
      if (!hasPermission(context, ACCESS_NETWORK_STATE)) {
        return true; // Assume we have a connection and try to upload.
      }
      ConnectivityManager cm = getSystemService(context, CONNECTIVITY_SERVICE);
      @SuppressLint("MissingPermission")
      NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
      return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    }
docs: Returns {@code true} if the phone is connected to a network, or if we don't have the enough
  permissions. Returns {@code false} otherwise.