简体   繁体   English

如何知道我的Android应用在用户设备上消耗了多少电量?

[英]How can I find out how much battery my Android app consumes on user's devices?

I am writing an app to evaluate the feasibility of a Bluetooth P2P network on Android. 我正在编写一个应用来评估Android上蓝牙P2P网络的可行性。

I noticed that on a Galaxy Nexus, it uses very little battery, while on a Nexus S, it drains the battery very quickly. 我注意到在Galaxy Nexus上,它使用非常少的电池,而在Nexus S上,它可以非常快速地耗尽电池。 This is directly caused by high CPU load due to Bluetooth. 这是由于蓝牙导致的高CPU负载直接导致的。

Now, I would like to collect information about how much battery drain my app causes on the devices it is installed upon. 现在,我想收集有关我的应用程序在其安装的设备上导致多少电池耗尽的信息。 Simply logging time vs. battery level is useless since I don't know when the device is in use, and even if I logged that, I would not know if the user is playing 3D games while Torrenting over WiFi or reading an eBook on a dimmed screen. 简单地记录时间与电池电量是没有用的,因为我不知道设备何时在使用,即使我记录了,我也不知道用户是在播放3D游戏而是通过WiFi播放或阅读电子书变暗的屏幕。

To make the problem worse, not all battery usage caused by my app is attributed to it in the battery screen - some is listed as "Bluetooth", for example (again, device-dependent). 为了使问题变得更糟,并非我的应用程序引起的所有电池使用都归因于电池屏幕 - 例如,有些被列为“蓝牙”(再次,取决于设备)。

Is there any easy, privacy-preserving way to get useful information on non-rooted devices? 是否有任何简单,隐私保护的方式来获取有关非根设备的有用信息? The devices are not under my control. 这些设备不在我的控制之下。 I cannot simply go look in the menu, I cannot use ADB. 我不能简单地在菜单中查看,我不能使用ADB。

The Android API doesn't support this currently. Android API目前不支持此功能。 The only available documentation is the one describing how to use BatteryManager 's broadcasts, which only supply the overall battery level and no per-application details. 唯一可用的文档是描述如何使用BatteryManager广播的文档 ,该广播仅提供整体电池电量而不是每个应用程序的详细信息。

I suppose that even if there are undocumented ways to emulate what the System Settings battery manager shows, they would require rooting. 我想即使有未记录的方法来模拟系统设置电池管理器显示的内容,它们也需要生根。

AFAIK there is no way to do this. AFAIK没有办法做到这一点。 The closest you can get to doing this is to hook up the device to a monitoring tool like little eye labs which does draw the battery consumed by your app over time on a graph. 最接近你可以做到这一点的是将设备连接到监控工具,如小眼科实验室 ,它会在图表上绘制应用程序消耗的电池。 It also supports marking key events in the app's lifetime like turning on WIFI / bluetooth etc. 它还支持在应用程序的生命周期中标记关键事件,如打开WIFI /蓝牙等。

There is no support for doing this remotely, but you can get the data on your own test devices. 不支持远程执行此操作,但您可以在自己的测试设备上获取数据。

This approach is quite crude, but it may help. 这种方法很粗糙,但可能有所帮助。 You can use the existing BatteryManager broadcasts to track changes to the overall battery level while your app is active . 您可以使用现有的BatteryManager广播来跟踪应用处于活动状态时对整体电池电量的更改。

Knowing when your app is active is a separate issue. 了解您的应用何时处于有效状态是一个单独的问题。 If your app consists entirely of Activities, then you can get good results by starting to track whenever any of your Activities' onResume() methods is called, and stopping tracking in onPause() . 如果您的应用程序完全由活动组成,那么您可以通过在调用任何活动的onResume()方法时开始跟踪并在onPause()停止跟踪来获得良好的结果。 If you can have all your Activities derive from a single base Activity class, then this is quite easy. 如果您可以从单个基本Activity类派生所有活动,那么这很容易。 See How to detect when an Android app goes to the background and come back to the foreground for suggestions on how to track this. 请参阅如何检测Android应用程序何时进入后台并返回前台以获取有关如何跟踪此问题的建议。

If your app is more complex, and the smart stuff happens in a longer-running component like a Service, then it's much harder. 如果你的应用程序更复杂,智能的东西发生在像服务这样运行时间更长的组件中,那么它就更难了。

This is crude, but at least it would help you distinguish "playing 3D games" from "using my app". 这很粗糙,但至少它可以帮助你区分“玩3D游戏”和“使用我的应用程序”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 怎么能找到我的android 2.3应用程序使用的堆内存多少? - How can find I out how much memory of the heap my android 2.3 app is using? 如何在Android中为我的应用收集电池信息? - how I can collect battery info for my app in android? 如何找出哪些设备与我的Android应用程序兼容(不兼容)? - How to find out which devices are (not) compatible with my Android App? 如何在我的Android应用程序中找出电池的使用情况 - How to find out usage of battery in my application in android 我的Android应用程序消耗太多内存 - My android app consumes too much memory Android中的服务会消耗大量电池 - Services in android consumes me much battery 我们如何计算/知道用户定义的应用程序使用了多少电池寿命? - How can we calculate/ to know, how much battery life my user defined application utilising? 当用户运行我的应用程序-Android时,如何知道电池是否由于过热而停止充电? - How can I know if the battery stop charging because of overheat, while the user run my application - Android? 如何避免三星设备的电池优化? (S6,S7和S8) - How I can avoid battery optimization of Samsung devices? (S6, S7 and S8) 如何为多个设备测试我的 android 应用程序? - How Can I test my android app for multiple devices?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM