检索的Android电池度量每n秒[高效] [英] Retrieving Android Battery Metrics Every n Seconds [Efficiently]
问题描述
我已经找到了监测电池电量许多来源,但他们没有说明如何在固定时间间隔检查。假设我要检查电池每隔n秒。什么是做到这一点的最有效方法是什么?
目前,我创建了一个广播接收器,在我与过滤器服务注册。然后我用一个ScheduledExecutor为取,从电池的信息。如果我没有理解什么是正确的事情,我做了广播接收器接收从电池所有广播,而在动态速率,因为他们进来,而不是恒定的速度我想在去检查一下。
看来,当我创建了广播接收器,接收与当前信息的初始的消息。会是更有效地创建一个接收器对象,每隔一段时间,收到此初始消息,然后摧毁它我要检查它每一次?或者说,有没有,我还没有想过以不同的方式?
来源,我用于监测电池,这样的情况下,如果有人有兴趣:
- 的http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
- http://developer.android.com/reference/android/os/ BatteryManager.html
- 电池的Android SDK中
假设我要检查电池每隔n秒。什么是做到这一点的最有效方法是什么?
块引用>除非
N
在数百或数千秒测量,你可能是电池寿命的#1的消费者。鉴于你的评论,我会认为你真的是每n分钟。第1步:建立
AlarmManager
时间表来调用IntentService
每个ñ
分钟(preferably不与_WAKEUP
报警类型)。第二步:在
IntentService
,电话registerReceiver()
与空
广播接收器
和的IntentFilter
这是ACTION_BATTERY_CHANGED
。返回值将是最后意图
直播本次活动。第三步:不要对数据进行处理
I have found numerous sources for monitoring battery levels, but none of them describe how to check it at a fixed time interval. Suppose I want to check the battery every n seconds. What is the most efficient way to do this?
Currently, I create a BroadcastReceiver and register it in my service with a filter. I then use a ScheduledExecutor to "fetch" the information from the battery. If I understand what's going on correctly, the BroadcastReceiver I made receives all broadcasts from the battery rather at a dynamic rate as they come in, rather than the constant rate I want to check it at.
It appears that when I create the BroadcastReceiver, it receives an "initial" message with the current info. Would it be more efficient to create a receiver object every so often, receive this initial message, then destroy it every time I want to check it? Or, is there a different way that I haven't thought about?
Sources that I used for monitoring the battery, in case if anybody is interested:
- http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
- http://developer.android.com/reference/android/os/BatteryManager.html
- Android Battery in SDK
解决方案Suppose I want to check the battery every n seconds. What is the most efficient way to do this?
Unless
n
is measured in hundreds or thousands of seconds, you may well be the #1 consumer of battery life. Given your comment, I will assume that you really mean "every n minutes".Step #1: Set up an
AlarmManager
schedule to invoke anIntentService
everyn
minutes (preferably not with a_WAKEUP
alarm type).Step #2: In that
IntentService
, callregisterReceiver()
with anull
BroadcastReceiver
and anIntentFilter
that is forACTION_BATTERY_CHANGED
. The return value will be the lastIntent
broadcast for this event.Step #3: Do something with the data.
这篇关于检索的Android电池度量每n秒[高效]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!