检索的Andr​​oid电池度量每n秒[高效] [英] Retrieving Android Battery Metrics Every n Seconds [Efficiently]

查看:106
本文介绍了检索的Andr​​oid电池度量每n秒[高效]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了监测电池电量许多来源,但他们没有说明如何在固定时间间隔检查。假设我要检查电池每隔n秒。什么是做到这一点的最有效方法是什么?

目前,我创建了一个广播接收器,在我与过滤器服务注册。然后我用一个ScheduledExecutor为取,从电池的信息。如果我没有理解什么是正确的事情,我做了广播接收器接收从电池所有广播,而在动态速率,因为他们进来,而不是恒定的速度我想在去检查一下。

看来,当我创建了广播接收器,接收与当前信息的初始的消息。会是更有效地创建一个接收器对象,每隔一段时间,收到此初始消息,然后摧毁它我要检查它每一次?或者说,有没有,我还没有想过以不同的方式?

来源,我用于监测电池,这样的情况下,如果有人有兴趣:


解决方案

  

假设我要检查电池每隔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:

解决方案

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 an IntentService every n minutes (preferably not with a _WAKEUP alarm type).

Step #2: In that IntentService, call registerReceiver() with a null BroadcastReceiver and an IntentFilter that is for ACTION_BATTERY_CHANGED. The return value will be the last Intent broadcast for this event.

Step #3: Do something with the data.

这篇关于检索的Andr​​oid电池度量每n秒[高效]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆