get the current battery level  [ 671 views ]

Goal: check the device battery level

In a static function like this:

public static float batteryPercent(){

  IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  Intent batteryStatus = getGlobalContext().registerReceiver(null, ifilter);

  int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
  int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

  float batteryPct = level / (float)scale * 100;

  return batteryPct;
}

The result is between 0-100 (0 is not very common 🙂 )

#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }