4

I can able to find out total battery percentage using BatteryManager.

I'm trying to implement the logic of how much amount of battery is consumed for each application which I'm opening until I'm closing the application.

Is it possible to do this using BatteryManger or any other services needs to be initiated?

macOsX
  • 447
  • 1
  • 9
  • 19

2 Answers2

3

This is because the calls to the power management APIs in Android are private and not exposed to the developer. These APIs are only available through Reflection.

The main packages that are used internally by Android regarding getting statistics from power management are held into com.android.internal.os.* (such as com.android.internal.os.BatteryStatsImpl).

StackOverflow user Chris Thompson shared a repository containing tools and an app for handling what you want. You can find it here.

You can inspire yourself from what he did and build something on top of that.

Also, it seems that you can get advanced battery statistics and CPU load used by each app using adb shell dumpsys batteryinfo.

A simple strace dumpsys batteryinfo while you are in adb shell might give you a hint on how these informations are retrieved by dumpsys.

Halim Qarroum
  • 13,985
  • 4
  • 46
  • 71
0

You could use a broadcast receiver to get information on battery data,i don't know how often you get that data,but i think at least it could be a starting point.

public class Main extends Activity {

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
   @Override
   public void onReceive(Context arg0, Intent intent) {
      int level = intent.getIntExtra("level", 0);
      //Do stuff with level
   }
};

@Override
public void onCreate(Bundle data) {
   super.onCreate(data);
   setContentView(R.layout.main);
   this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  }
}
sokie
  • 1,966
  • 22
  • 37