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);
}
退房這段代碼...
的可能重複[我可以查看iPhone的電池狀態在後臺狀態?(http://stackoverflow.com/questions/7705373/can-i-check-battery-status-of-iphone-in-background-state) – Pang