且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在android中实现应用程序的电池使用量计算?

更新时间:2022-10-15 13:04:38

这是因为对Android中的电源管理API的调用是私有的,不会向开发人员公开.这些API仅可通过 Reflection 使用.

Android内部用于从电源管理获取统计信息的主要软件包都保存在 com.android.internal.os.* 中(例如 com.android.internal.os.BatteryStatsImpl ).

***用户Chris Thompson共享了一个包含工具和用于处理所需内容的应用程序的存储库.您可以在此处找到它.

您可以从他的所作所为中激发自己,并在此基础上有所建树.

另外,似乎您可以使用 adb shell dumpsys batteryinfo 获得每个应用程序使用的高级电池统计信息和CPU负载.

adb shell 中时,一个简单的 strace dumpinfo batteryinfo 可能会提示您如何通过 dumpsys 检索这些信息./p>

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?

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).

*** 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.