code: protected static Boolean isCharging(Context ctx) {
          try {
              Intent intent = ctx.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
              if (intent == null) {
                  return null;
              }
  
              int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
              return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
          } catch (Exception e) {
              Log.e(TAG, "Error getting device charging state.", e);
              return null;
          }
      }
docs: Checks whether or not the device is currently plugged in and charging, or null if unknown.
  
  @param ctx Android application context
  @return whether or not the device is currently plugged in and charging, or null if unknown