Using the inner class BatteryStatsImpl via Reflection in android

I am trying to get a list of running applications and the number of batteries used by each of them. I have google for a long time, but have not come up with a solution. However, there were several references to the internal PowerProfile, PowerUsageSummary classes.

I used them in the Reflection technique, but did not get what I was looking for. PowerUsageSummary shows the same details that you can see by going to the device settings-> Applications-> Battery usage (this can be seen on the Samsund device).

Then I used the PowerProfile class, but only got the mA of current used by WIFI, AUDIO, VIDEO, GPS, BLUETOOTH, etc. (The mA values ​​do not change so often. I'm not sure that the values ​​are correct). Another reference was the BatteryStatsImpl class. I tested this class, but the values ​​are always 0. However, I am looking for a list of running applications and the number of batteries used by each of them. Any help is appreciated.

Thank,

Here is an example of the code I tried for the BatteryStatsImpl class.

        String BATTERY_PROFILE_CLASS = "com.android.internal.os.BatteryStatsImpl";
        Object mBatteryProfile = Class.forName(BATTERY_PROFILE_CLASS).getConstructor().newInstance();
        Method batteryMeth = Class.forName(BATTERY_PROFILE_CLASS).getMethod("getBatteryUptime", long.class);
        Object arglist1[] = new Object[1];
        arglist1[0] = System.currentTimeMillis();
        // This is to calculate the batteryUpTime since the current time.
        Long batteryUptime = (Long) batteryMeth.invoke(mBatteryProfile, arglist1);

        Method dischargeMeth = Class.forName(BATTERY_PROFILE_CLASS).getMethod("getDischargeStartLevel");
        // This is to calculate the dischargeTime of the device battery
        Integer dischargeTime = (Integer) dischargeMeth.invoke(mBatteryProfile);
+5
source share
2 answers

First of all, please note that you cannot use this API if you are not installed on the system image, and therefore you can hold the BATTERY_STATS permission. This is not available for third-party applications installed separately from the system.

, BatteryStatsImpl. , BatteryStatsService. , : https://code.google.com/p/android-source-browsing/source/browse/src/com/android/settings/fuelgauge/PowerUsageSummary.java?repo=platform--packages--apps--settings

:

import android.os.BatteryStats;

import com.android.internal.app.IBatteryStats;
import com.android.internal.os.BatteryStatsImpl;

IBatteryStats mBatteryInfo;
UserManager mUm;
BatteryStatsImpl mStats;

mBatteryInfo = IBatteryStats.Stub.asInterface(
        ServiceManager.getService("batteryinfo"));

private void load() {
    try {
        byte[] data = mBatteryInfo.getStatistics();
        Parcel parcel = Parcel.obtain();
        parcel.unmarshall(data, 0, data.length);
        parcel.setDataPosition(0);
        mStats = com.android.internal.os.BatteryStatsImpl.CREATOR
                .createFromParcel(parcel);
        mStats.distributeWorkLocked(BatteryStats.STATS_SINCE_CHARGED);
    } catch (RemoteException e) {
        Log.e(TAG, "RemoteException:", e);
    }
}
+7

BatteryStatsService . , BatteryStatsImpl ( note*() , )

,

0

All Articles