iPhone4 iOS5电池电量监控我需要添加setBatteryMonitoringEnabled:NO来定期进行电池检查吗? [英] iPhone4 iOS5 battery level monitoring do I need to add setBatteryMonitoringEnabled:NO to periodic battery checks?

查看:550
本文介绍了iPhone4 iOS5电池电量监控我需要添加setBatteryMonitoringEnabled:NO来定期进行电池检查吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个应用程序,可以在一夜之间对加速计和陀螺仪数据进行采样。这是一个非常耗电的操作,我想教我的应用程序识别电池电量不足的时间。

I'm running an app that samples accelerometer and gyroscope data overnight. This is a very battery intensive operation, and I would like to teach my app to recognize when the battery is getting low.

这是我的原型代码,每10分钟检查一次电池水平

Here's my prototype code, which checks the battery level every 10 minutes

NSDate* date = [NSDate date];
      if((int)([date timeIntervalSinceReferenceDate])%600 == 0)
            {
                UIDevice *myDevice = [UIDevice currentDevice];
                [myDevice setBatteryMonitoringEnabled:YES];
                float batLeft = [myDevice batteryLevel]; 
                int batinfo=(batLeft*100);

            [self postStatusMessageWithTitle:nil
                                 description:[NSString stringWithFormat:@"%@ battery level: %i",[dateFormat stringFromDate:dateShow],batinfo]];
                [myDevice setBatteryMonitoringEnabled:NO];
            }

我的问题是:我需要将此行添加到结尾代码:

My question is this: do I need to add this line to the end of the code:

[myDevice setBatteryMonitoringEnabled:NO];

看来电池检查是在那里执行的,没有异步委托调用。将值设置为NO会因为不必在一夜之间监控电池电量而节省电池电量吗?我可以通过将其设置为NO来解决任何问题吗?

It appears that the battery check is performed right there, there are no asynchronous delegate calls. Will setting the value to NO produce any battery savings from not having to monitor the battery level overnight? Will I break anything by setting it to NO?

感谢您的任何输入!

推荐答案

通常认为最好的做法是避免轮询,而是要求系统发出通知,如下所示:

It's usually considered best practice to avoid polling and instead ask for notifications from the system, like so:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[[NSNotificationCenter defaultCenter]
        addObserver:self
           selector:@selector(batteryLevelUpdate)
               name:UIDeviceBatteryLevelDidChangeNotification
             object:nil];

...其中 batteryLevelUpdate 看起来像这样:

…where the batteryLevelUpdate would look like this:

- (void)batteryLevelUpdate:(NSNotification *)notification
{
    // do whatever, using: [[UIDevice currentDevice] batteryLevel]
}

来自 UIDevice Class Reference


电池电平变化通知的发送频率不会超过每分钟一次。不要试图计算电池排水率或电池剩余时间;根据内置应用程序和应用程序,排水率可能会经常变化。

Notifications for battery level change are sent no more frequently than once per minute. Do not attempt to calculate battery drainage rate or battery time remaining; drainage rate can change frequently depending on built-in applications as well as your application.

每分钟一次比你的代码多10倍目前正在检查,同时在CPU方面付出的努力要少得多。它提及,但是更改的粒度会导致通知 - 发送的变化是0.01%,还是需要> 1%的变化?

Once per minute is 10x more often than your code is currently checking while exerting much less effort CPU-wise. It does not mention, however what granularity of changes will cause notifications -- is a 0.01% change going to send, or does it require >1% change?

如果您要将 setBatteryMonitoringEnabled 设置回,请回答您的其他问题:如果您正在使用通知而不是手动轮询batteryStatus然后答案是你必须将它保留在 YES ,否则可能会丢失通知。

To answer your other question if you should set the setBatteryMonitoringEnabled back to NO : if you are using the notifications and not manually polling for batteryStatus then the answer is that you must leave it at YES, or risk missing the notifications.

Apple's官方BatteryStatus示例代码使用相同的电池状态报告。

Apple's official BatteryStatus Sample Code uses this same take on a battery-status reporting.

还有 UIDeviceBatteryStateDidChangeNotification 当设备放电(使用中),充电或充满电时,它会通知您。

There is also a UIDeviceBatteryStateDidChangeNotification which will notify you when a device is discharging (in use), charging or has become fully charged.

这篇关于iPhone4 iOS5电池电量监控我需要添加setBatteryMonitoringEnabled:NO来定期进行电池检查吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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