iPhone4 iOS5 电池电量监控是否需要将 setBatteryMonitoringEnabled:NO 添加到定期电池检查?

发布于 2024-12-14 13:02:06 字数 927 浏览 0 评论 0原文

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

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

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];
            }

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

[myDevice 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.

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];

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?

Thank you for any input!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2024-12-21 13:02:06

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

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

…其中 batteryLevelUpdate 如下所示:

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

来自 UIDevice 类参考

电池电量变化通知的发送频率不超过每分钟一次。请勿尝试计算电池消耗率或电池剩余时间;排水率可能会经常变化,具体取决于内置应用程序以及您的应用程序。

每分钟一次的频率比您的代码当前检查的频率高 10 倍,同时在 CPU 方面的工作量要少得多。它没有提及,但是什么粒度的更改会导致通知 - 是要发送 0.01% 的更改,还是需要 >1% 的更改?

要回答您是否应该将 setBatteryMonitoringEnabled 设置回 NO 的其他问题:如果您正在使用通知而不是手动轮询电池状态,那么答案是您必须保留它YES,否则可能会错过通知。

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

还有一个 UIDeviceBatteryStateDidChangeNotification ,它会在设备放电(使用中)、充电或已充满电时通知您。

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];

…where the batteryLevelUpdate would look like this:

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

From the 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.

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?

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 official BatteryStatus Sample Code uses this same take on a battery-status reporting.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文