- (IBAction)buttonPressed:(id)sender
{
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
[self beingBackgroundUpdateTask];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];
[self currentBatteryState];
}
- (void) beingBackgroundUpdateTask
{
self->backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
- (void) endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self->backgroundUpdateTask];
self->backgroundUpdateTask = UIBackgroundTaskInvalid;
}
由於某種原因,通知並未被遵守。難道我做錯了什麼?我想在拔掉電源時觀察10分鐘iOS MultiTasking無法正常工作
如何確定UIDevice是否在後臺發送通知? – 2012-07-07 17:52:09
我會嘗試修復我先建議的兩件事,然後測試以查看這些通知是否在後臺到達。 – 2012-07-07 17:53:43
我根據您的建議修改了代碼。它似乎沒有工作 – 2012-07-07 17:54:36