【问题标题】:iOS Application for Check Battery level in background用于在后台检查电池电量的 iOS 应用程序
【发布时间】:2013-10-01 05:05:06
【问题描述】:

是否可以在我们的应用程序处于后台时检查电池电量?

目前我正在开发一个 iOS 应用程序,当电池达到一定水平时,用户会收到警报。

我在 google/stake overflow 上进行了搜索。但发现没有任何用处。我可以在应用程序处于前台状态时确定电池电量。但是我们都知道苹果不允许应用程序在后台运行。

所以我的问题是,即使我的应用程序在后台,我如何才能获得当前的电池电量事件。

这是类似的应用程序。它是一个付费应用程序。但是通过查看屏幕截图,您会明白我想说的意思。

https://itunes.apple.com/it/app/battery-alert!/id416283462?mt=8

【问题讨论】:

标签: ios ios7 battery


【解决方案1】:
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];

// Your notification handler
- (void)batteryChanged:(NSNotification *)notification
{
    UIDevice *device = [UIDevice currentDevice];
    NSLog(@"state: %i | charge: %f", device.batteryState, device.batteryLevel);
}

查看此代码...

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多