1

I need help. I just want to make application that reads battery capacity, like read in mAh/mA. Anyone can help me please?

I've read another thread about this, but I was confused because I need an integer from battery capacity. For example, my android has a battery with capacity 2500 mAh and I need that integer of capacity(2500) where I want to include that number in my calculation.

Thanks for the help.

This is code that I want to change, I am just confused where it must be changed.

public void getBatteryCapacity() {
        Object mPowerProfile_ = null;

        final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

        try {
            mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS)
                    .getConstructor(Context.class).newInstance(this);
        } catch (Exception e) {
            e.printStackTrace();
        } 

        try {
            double batteryCapacity = (Double) Class
                    .forName(POWER_PROFILE_CLASS)
                    .getMethod("getAveragePower", java.lang.String.class)
                    .invoke(mPowerProfile_, "battery.capacity");
            Toast.makeText(MainActivity.this, batteryCapacity + " mah",
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
  • I'm not really an Android guy, but I'm guessing that `getAveragePower()` returns the current average draw. It's not returning capacity; it's returning how much is being used by the system in mAh – Lynn Crumbling Mar 09 '14 at 17:12
  • Perhaps, can you use this service http://developer.android.com/reference/android/os/BatteryManager.html – Guillaume Mar 09 '14 at 17:13
  • LynnCrumbling: oke thanks,can you give me specific way? @ebuprofen: i've read, but i confused, coz i very newbie in android, – user3398970 Mar 09 '14 at 17:22
  • See this: http://stackoverflow.com/a/19152265/2611927 – Hardy Mar 09 '14 at 17:29
  • i see, but my problem is how to declare integer from capacity battery? i am sorry if my question is detail, – user3398970 Mar 09 '14 at 17:41
  • @Hardy: do you can help with detail? i very very thanks if you help me – user3398970 Mar 09 '14 at 17:45

1 Answers1

4

Yes, your code gives total mAh capacity. You can change that function to return the value like this:

public Double getBatteryCapacity() {

  // Power profile class instance
  Object mPowerProfile_ = null;

  // Reset variable for battery capacity
  double batteryCapacity = 0;

  // Power profile class name 
  final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

  try {

    // Get power profile class and create instance. We have to do this 
    // dynamically because android.internal package is not part of public API
    mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS)
                    .getConstructor(Context.class).newInstance(this);

  } catch (Exception e) {

    // Class not found?
    e.printStackTrace();
  } 

  try {

    // Invoke PowerProfile method "getAveragePower" with param "battery.capacity"
    batteryCapacity = (Double) Class
                    .forName(POWER_PROFILE_CLASS)
                    .getMethod("getAveragePower", java.lang.String.class)
                    .invoke(mPowerProfile_, "battery.capacity");

  } catch (Exception e) {

    // Something went wrong
    e.printStackTrace();
  } 

    return batteryCapacity;
 }

The getAveragePower function returns the average current in mA consumed by the subsystem. In this case subsystem string is battery.capacity which returns the battery capacity.

See class code here: https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/com/android/internal/os/PowerProfile.java

And if you really want that value as int, just change it like this:

int bc = getBatteryCapacity().intValue();
Hardy
  • 5,590
  • 2
  • 18
  • 27
  • i have an error after i declare int bc = (int)getBatteryCapacity();. the is cannot declare double into int,how i can fix it? – user3398970 Mar 10 '14 at 01:51
  • but after i change it from (int) to (double) not error, but after i run it works, but that code 1000mAh, actually my device have 2500 mAh, where's the error? – user3398970 Mar 10 '14 at 02:04
  • Seems that you are quite new with Java and programming.. changed my code to work with int. Double is just decimal number and int whole number. I tested this code with my Samsung Galaxy S4 and it gave right number. Not sure why it gives you wrong number. – Hardy Mar 10 '14 at 11:28
  • @Hardy at least in some devices it seems to read false values indeed. 1000mAh when it should say 2500mAh. Wonder why that happens... – HenriqueMS Apr 17 '17 at 11:37