Sunday, March 4, 2018

Android - Get battery capacity via reflection



double batteryCapacity = 0.f;

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 {
    batteryCapacity = (Double) Class
            .forName(POWER_PROFILE_CLASS)
            .getMethod("getAveragePower", java.lang.String.class)
            .invoke(mPowerProfile_, "battery.capacity");
} catch (Exception e) {
    e.printStackTrace();
}

No comments: