java 我的 Android 应用会耗尽电池电量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5124013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:34:08  来源:igfitidea点击:

Is my Android App Draining Battery?

javaandroidoptimizationbattery

提问by Tom R

I'm developing a game for Android. It uses a surface view and uses the sort of standard 2D drawing APIs provided. When I first released the game, I was doing all sorts of daft things like re-drawing 9-patches on each frame and likewise with text. I have since optimised much of this by drawing to Bitmap objects and drawing them each frame, only re-drawing onto the Bitmap objects when required.

我正在为 Android 开发游戏。它使用表面视图并使用提供的那种标准 2D 绘图 API。当我第一次发布游戏时,我正在做各种愚蠢的事情,比如在每一帧上重新绘制 9 个补丁,同样的文本。从那以后,我通过绘制 Bitmap 对象并在每一帧绘制它们来优化大部分内容,仅在需要时重新绘制到 Bitmap 对象上。

I've received complaints about battery drain before, and following my modifications I'd like to know (scientifically) if I've made any improvements. Unfortunately, I don't have any prior data to go by, so it would be most useful to compare the performance to some other game.

我之前收到过关于电池耗尽的投诉,在我进行修改之后,我想(科学地)知道我是否做了任何改进。不幸的是,我没有任何先前的数据可供参考,因此将性能与其他游戏进行比较是最有用的。

I've been running Traceview, and using the results of it mostly for the purposes of identifying CPU-time-consuming methods.

我一直在运行 Traceview,并将其结果主要用于识别 CPU 耗时的方法。

So -- what's the best way of determining my app's battery performance, and what's a good benchmark?

那么 -确定我的应用程序电池性能的最佳方法是什么,什么是好的基准?

I know I can look at the %s of different apps through the settings, but this is again unscientific, as the figure I get from this also depends on what's happening in all of the other apps. I've looked through (most of) Google's documentation, and although the message is clear that you shouldbe saving battery (and it gives the occasional tip as to how), there is little indication of how I can measure how well my app is performing. The last thing I want are more complaints of battery drain in the Android Market!

我知道我可以通过设置查看不同应用程序的 %s,但这又是不科学的,因为我从中得到的数字还取决于所有其他应用程序中发生的情况。我已经浏览了(大部分)谷歌的文档,虽然消息很清楚你应该节省电池(它偶尔会给出关于如何节省电池的提示),但几乎没有迹象表明我如何衡量我的应用程序有多好表演。我最不想要的是更多关于 Android Market 中电池消耗的抱怨!

Thanks in advance.

提前致谢。



EDIT

编辑

Thanks for all your helpful advice/suggestions. What I really want to know is how I can use the data coming from Traceview (ie: CPU time in ms spent on each frame of the game) to determine battery usage(if this is at all possible). Reading back on my original question, I can see I was a bit vague. Thanks again.

感谢您提供所有有用的建议/建议。我真正想知道的是如何使用来自 Traceview 的数据(即:在游戏的每一帧上花费的 CPU 时间(以毫秒为单位))来确定电池使用情况(如果可能的话)。回顾我最初的问题,我可以看出我有点含糊。再次感谢。

回答by Kingsolmn

Here is my suggestion:

这是我的建议:

I watch power consumption while developing my apps (that sometimes poll the sensors at rates of <25ns) using PowerTutor. Check it out, it sounds like this maybe what you are looking for, the app tells you what you are using in mW, J, or relative to the rest of the system. Also, results are broken down by CPU, WiFi, Display, (other radios installed). The only catch is that it is written for a specific phone model, but I use it with great success on my EVO 4G, Galaxy S (Sprint Epic), and Hero.

我在使用PowerTutor开发我的应用程序(有时以 <25ns 的速率轮询传感器)时观察功耗。检查一下,听起来这可能是您正在寻找的内容,该应用程序会告诉您您正在使用的 mW、J 或相对于系统其余部分的内容。此外,结果按 CPU、WiFi、显示器(安装的其他无线电)进行细分。唯一的问题是它是为特定手机型号编写的,但我在我的 EVO 4G、Galaxy S(Sprint Epic)和 Hero 上使用它取得了巨大成功。

Good luck, -Steve

祝你好运,-史蒂夫

回答by Wroclai

There is a possibility that your game is draining battery. I believe this depends on several reasons, which reads as follows:

您的游戏可能正在耗尽电池电量。我相信这取决于几个原因,其内容如下:

  • Your application is a game. Games drains battery quickly.
  • You're iteratingwith help from a Thread. Have you limited the FPS to make the CPU skip unnecessary iterations? Since you're working with 2D I assume you're using the SurfaceView. 60 FPS will be enough for a real-time game.
  • You don't stopthe Threadwhen your application terminates. Hence you reiterate code when your application isn't alive.
  • Have you an iterate lockthat does wait();during onPause?
  • 您的应用程序是一个游戏。游戏耗电很快。
  • 您正在.a的帮助下进行迭代Thread。您是否限制了 FPS 以使 CPU 跳过不必要的迭代?由于您使用的是 2D,因此我假设您使用的是SurfaceView. 对于实时游戏来说,60 FPS 就足够了。
  • 你不停止Thread,当你的应用程序终止。因此,当您的应用程序不活跃时,您会重申代码。
  • 你有一个迭代锁,做wait();onPause

The people commenting that your game is leaking battery probably aims when your application isn't in use. Otherwise, it would be wierd because every game on Android Market drains battery - more orless.

评论您的游戏电池漏电的人可能是在您的应用程序未使用时提出的。否则,这将是奇怪的,因为在Android Market每场比赛水渠电池-更多更少。

回答by EboMike

If you're trying to gauge the "improvement over your previous version", I don't think it makes sense to compare to another game! Unless those two games do the exact thing, this is as unscientific as it gets.

如果您想衡量“对您之前版本的改进”,我认为与其他游戏进行比较是没有意义的!除非这两款游戏完全一样,否则这是不科学的。

Instead, I would grab the previous version of your app from source control, run it, measure it, and then run it with the latest code and compare it again.

相反,我会从源代码管理中获取应用程序的先前版本,运行它,测量它,然后使用最新代码运行它并再次进行比较。

To compare, you could for example use the command line tool "top" (definitely available in busybox if your phone is rooted, not sure if it comes with a stock phone. Probably not). That shows you the CPU usage of your process in percent.

为了进行比较,您可以例如使用命令行工具“top”(如果您的手机已root,则肯定可以在busybox中使用,不确定它是否带有普通手机。可能不是)。这会以百分比形式显示进程的 CPU 使用率。

回答by Murat

There is a battery profiler developed by Qualcomm called Trepn Profiler: https://developer.qualcomm.com/mobile-development/increase-app-performance/trepn-profiler

有一个高通公司开发的电池分析器叫 Trepn Profiler:https: //developer.qualcomm.com/mobile-development/increase-app-performance/trepn-profiler

回答by Kingsolmn

how I can use the data coming from Traceview (ie: CPU time in ms spent on each frame of the game) to determine battery usage (if this is at all possible)

我如何使用来自 Traceview 的数据(即:在游戏的每一帧上花费的 CPU 时间(以毫秒为单位))来确定电池使用情况(如果可能的话)

In theory it would be possible to extrapolate the battery usage for your app by looking at the power consumption on a frame by frame basis. The best way to accomplish this would be to evaluate the power consumption of the CPU (only) for a given period (say two seconds) while your app is running the most CPU intensive operation, (additionally, GPU power usage could be gleaned this way also) while recording TraceView data (such as frames per second or flops per second) giving you the the traffic across the CPU/GPU for a given millisecond. Using this data you could accurately calculate the average peak power consumption for your app by running the above test a few times.

从理论上讲,可以通过逐帧查看功耗来推断应用程序的电池使用情况。实现此目的的最佳方法是在您的应用程序运行 CPU 密集程度最高的操作时,评估给定时间段(例如两秒)内 CPU(仅)的功耗(此外,可以通过这种方式收集 GPU 功耗)也)同时记录 TraceView 数据(例如每秒帧数或每秒触发器数),为您提供给定毫秒内 CPU/GPU 上的流量。使用此数据,您可以通过多次运行上述测试来准确计算应用程序的平均峰值功耗。

Here is why I say it is theory only: There are many variables to consider:

这就是我说它只是理论的原因:有许多变量需要考虑:

  1. The number and nature of other processes running at the time of the above test (processor intensive)
  2. Method of evaluating the power draw across the CPU/GPU (while tools such as PowerTutor are effective for evaluating power consumption, in this case the evaluation would not be as effective because of the need to collect time stamped power usage data. Additionally, just about any method of collecting power data would introduce an additional overhead (Schr?dinger's cat) but that strictly depends on the level of accuracy you require/desire.)
  3. The reason for the power consumption information - If you are looking to define the power consumption of your app for testing or BETA testing/evaluation purposes then it is a feasible task with some determination and the proper tools. If you are looking to gain usable information about power consumption "in the wild", on user's devices, then I would say it is plausible but not realistic. The vairables involved would make even the most determined and dedicated researcher faint. You would have to test on every possible combination of device/Android version in the wild. Additionally, the combinations of running processes/threads and installed apps is likely incalculable.
  1. 上述测试时运行的其他进程的数量和性质(处理器密集型)
  2. 评估 CPU/GPU 功耗的方法(虽然 PowerTutor 等工具可有效评估功耗,但在这种情况下,评估不会那么有效,因为需要收集带时间戳的功耗数据。此外,大约任何收集功率数据的方法都会带来额外的开销(薛定谔的猫),但这严格取决于您要求/期望的准确度水平。)
  3. 功耗信息的原因 - 如果您希望为测试或 BETA 测试/评估目的定义应用程序的功耗,那么通过一些确定和适当的工具,这是一项可行的任务。如果您想在用户的设备上“在野外”获得有关功耗的可用信息,那么我会说这是有道理的,但不现实。所涉及的变量甚至会使最坚定和最敬业的研究人员晕倒。您必须在野外测试每种可能的设备/Android 版本组合。此外,正在运行的进程/线程和已安装的应用程序的组合可能无法计算。

I hope this provides some insight to your question, although I may have gone deeper into it than needed.

我希望这可以为您的问题提供一些见解,尽管我可能比需要的更深入。

-Steve

-史蒂夫

回答by AllDayAmazing

For anyone looking, one resource we've been using that is extremely helpful is a free app from AT&T called ARO.

对于任何人来说,我们一直在使用的一个非常有用的资源是来自 AT&T 的名为 ARO 的免费应用程序。

Give it a look: ARO

看一看:ARO

It has helped me before and I don't see it mentioned very often so thought I'd drop it here for anyone looking.

它以前对我有帮助,但我不经常看到它被提及,所以我想我会把它放在这里供任何人寻找。

回答by brunorey

I know this question is old and it's late, but for anyone who comes here looking for a solution I suggest you take a look at JouleUnit test: http://dnlkntt.wordpress.com/2013/09/28/how-to-test-energy-consumption-on-android-devices/

我知道这个问题很旧而且已经很晚了,但是对于来这里寻找解决方案的人,我建议您查看 JouleUnit 测试:http://dnlkntt.wordpress.com/2013/09/28/how-to- test-energy-consumption-on-android-devices/

It integrates into eclipse and gives you a great amount of detail about how much battery your app is consuming.

它集成到 eclipse 中,并为您提供有关应用程序消耗多少电池的大量详细信息。

回答by Paiman Roointan

I know of three options that can help you for a having scientific measure:

我知道可以帮助您进行科学测量的三个选项:

  1. Use a hardware specifically built for this. Monsoon HIGH VOLTAGE POWER MONITOR. https://msoon.github.io/powermonitor/PowerTool/doc/Power%20Monitor%20Manual.pdf
  2. Download and install Trepn Profiler (a tools from Qualcomm) on your phone. You wont need a computer for reporting. Reports are live and realtime on the phone. You can download Trepn Profiler from the following link: https://play.google.com/store/apps/details?id=com.quicinc.trepn&hl=en_US
  1. 使用专门为此构建的硬件。季风高压电源监视器。 https://msoon.github.io/powermonitor/PowerTool/doc/Power%20Monitor%20Manual.pdf
  2. 在您的手机上下载并安装 Trepn Profiler(来自 Qualcomm 的工具)。您不需要计算机来报告。报告在电话上是实时和实时的。您可以从以下链接下载 Trepn Profiler:https: //play.google.com/store/apps/details?id=com.quicinc.trepn &hl=en_US

Please take note that for recent phone (with android 6+) it works in estimation mode. If you need accurate numbers, you need to a list of select devices. Check the following link for the list: https://developer.qualcomm.com/software/trepn-power-profiler/faqYou can profile apps separately, and the whole system.

请注意,对于最近的手机(使用 android 6+),它在估算模式下工作。如果您需要准确的数字,则需要选择设备列表。检查以下链接以获取列表:https: //developer.qualcomm.com/software/trepn-power-profiler/faq您可以单独分析应用程序和整个系统。

  1. Use Batterystats and Battery Historian from google. https://developer.android.com/studio/profile/battery-historian
  1. 使用来自谷歌的 Batterystats 和 Battery Historian。 https://developer.android.com/studio/profile/battery-historian

回答by TimFoolery

"I know I can look at the %s of different apps through the settings, but this is again unscientific, as the figure I get from this also depends on what's happening in all of the other apps."

“我知道我可以通过设置查看不同应用程序的 %s,但这又是不科学的,因为我从中得到的数字还取决于所有其他应用程序中发生的情况。”

The first thing I'd do is hunt for an app already out there that has a known, consistent battery usage, and then you can just use that as a reference to determine your app's usage.

我要做的第一件事是寻找已经存在的应用程序,该应用程序具有已知的、一致的电池使用情况,然后您可以将其用作参考来确定您的应用程序的使用情况。

If there is no such app, you will have to hope for an answer from someone else... and if you are successful making such an app, I would suggest selling your new "battery usage reference" app so that other programmers could use it. :)

如果没有这样的应用程序,您将不得不希望其他人的回答......如果您成功制作了这样的应用程序,我建议出售您的新“电池使用参考”应用程序,以便其他程序员可以使用它. :)