code: protected static Float getBatteryLevel(Context ctx) {
          try {
              Intent intent = ctx.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
              if (intent == null) {
                  return null;
              }
  
              int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
              int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
  
              if (level == -1 || scale == -1) {
                  return null;
              }
  
              // CHECKSTYLE.OFF: MagicNumber
              float percentMultiplier = 100.0f;
              // CHECKSTYLE.ON: MagicNumber
  
              return ((float) level / (float) scale) * percentMultiplier;
          } catch (Exception e) {
              Log.e(TAG, "Error getting device battery level.", e);
              return null;
          }
      }
docs: Get the device's current battery level (as a percentage of total).
  
  @param ctx Android application context
  @return the device's current battery level (as a percentage of total), or null if unknown