get Battery Percent - Android android.os

Android examples for android.os:Battery

Description

get Battery Percent

Demo Code

import android.content.Intent;
import android.os.BatteryManager;

public class Main {

  public static int getBatteryPercent(Intent intent) {
    if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {

      int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

      int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

      return (level * 100) / scale;
    } else {/* w  ww . j a v a  2  s  .  co m*/
      return 0;
    }
  }

}

Related Tutorials