code: public static boolean isConnectedToWiFiOrMobileNetwork(Context context) {
      final String service = Context.CONNECTIVITY_SERVICE;
      final ConnectivityManager manager = (ConnectivityManager) context.getSystemService(service);
      final NetworkInfo networkInfo = manager.getActiveNetworkInfo();
  
      if (networkInfo == null) {
        return false;
      }
  
      final boolean isWifiNetwork = networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
      final boolean isMobileNetwork = networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
  
      if (isWifiNetwork || isMobileNetwork) {
        return true;
      }
  
      return false;
    }
docs: Helper method, which checks if device is connected to WiFi or mobile network.
  
  @param context Activity or application context
  @return boolean true if is connected to mobile or WiFi network.