安卓省电

标签 android service save battery

我有一个包含服务和主要 Activity 的应用程序。该服务运行一个倒计时计时器,并向接收事件的 Activity 发送一个偶数,以在屏幕上绘制/刷新倒计时计时器。很简单。现在我正在调查电池使用情况,这是非常非常高的

我打算将计时器控制时间从 200 毫秒减少到 800 毫秒,并将可运行的服务线程从 500 减少到 800。我怀疑这一定会节省电池电量。

但是有几个问题:

  1. 我的主要问题:当屏幕锁定时(我有部分唤醒锁定),android 会继续更新屏幕吗?如果是这样,我可以尝试在屏幕关闭时不发送刷新信息(我如何检测屏幕是否已锁定?)。您认为这有帮助吗?

  2. 您是否知道编程或应用设计中节省电量的最佳做法?我一直在寻找关于它的网页,但没有找到任何有趣的东西。

  3. 有数据库打开废电池吗?

谢谢

最佳答案

1:您可以使用 Alarmmanager 来执行更新。 (看看这里:android appwidgets

Note: If the device is asleep when it is time for an update (as defined by updatePeriodMillis), then the device will wake up in order to perform the update. If you don't update more than once per hour, this probably won't cause significant problems for the battery life. If, however, you need to update more frequently and/or you do not need to update while the device is asleep, then you can instead perform updates based on an alarm that will not wake the device. To do so, set an alarm with an Intent that your AppWidgetProvider receives, using the AlarmManager. Set the alarm type to either ELAPSED_REALTIME or RTC, which will only deliver the alarm when the device is awake. Then set updatePeriodMillis to zero ("0").

这应该可用于小部件和“正常” Activity 。

2:我不知道官方文档。但是您应该问自己以下问题:是否有必要经常更新?如果距离 Activity 还有 5 小时,您是否需要每秒或每分钟更新一次 GUI?您可以级联更新:事件越接近,更新就越频繁。

3:我认为不是。

关于安卓省电,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8254183/

相关文章:

.net - 将数据注入(inject) WCF 服务

java - BroadcastReceiver 意外调用了 onReceive()

android - 如何在Android中编辑XML并保存?

android - 仅在 android 中的 Lollipop 5.0 中显示带有月份和年份的日期选择器

android - 为什么我得到 : null cannot be cast to non-null type android. widget.SearchView ?

android - Google FusedLocationApi API 调用 onConnectionSuspended 的时间太多,我无法获取 GPS 位置

ios - 无法保存数据 --- Swift、IOS、Core 数据 --- 线程 1 : signal SIGABRT

Java如何提示用户保存word文档

android - LinearLayout 中的项目居中

android - android :layout_column do?到底是什么