我在我的应用程序中使用了GPS位置更新.我想检测iOS设备是否处于睡眠模式,这样我就可以关闭GPS位置更新并优化电池使用.我已经在iOS 6中尝试了pausesLocationupdates,但它无法正常工作.我想在设备进入睡眠模式后立即关闭GPS位置更新.我想检测设备中的锁定/解锁事件.
有没有办法实现这个功能?
到目前为止,我收到了下面给出的达尔文通知
-(void)registerForall
{
//Screen lock notifications
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.iokit.hid.displayStatus"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.springboard.lockstate"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.springboard.hasBlankedScreen"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.springboard.lockcomplete"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
}
//call back
static void displayStatusChanged(CFNotificationCenterRef center, …Run Code Online (Sandbox Code Playgroud) 我想实现一个功能,我使用GPS进行更新,当用户不移动时,我希望暂停GPS更新.根据" 与位置服务保持同步 "视频,我这样做了:
//Configure Location Manager
self.theLocationManager = [[CLLocationManager alloc]init];
[self.theLocationManager setDelegate:self];
[self.theLocationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.theLocationManager setActivityType:CLActivityTypeFitness];
NSLog(@"Activity Type Set: CLActivityTypeFitness");
[self.theLocationManager setPausesLocationUpdatesAutomatically:YES];
[self.theLocationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)
代表:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
NSLog(@"Started location Updates");
}
- (void)locationManagerDidPauseLocationUpdates:(CLLocationManager *)manager
{
NSLog(@"Pausing location Updates");
}
- (void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager
{
NSLog(@"Resuming location Updates");
UIAlertView *pop = [[UIAlertView alloc]initWithTitle:@"Info" message:@"Location Updates resumed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[pop show];
[pop release];
}
Run Code Online (Sandbox Code Playgroud)
我没有得到所需的行为,当设备空闲时,代理人"didPause .."和"didResume ..."甚至没有被调用.
理想情况下,GPS更新必须停止和恢复,具体取决于设备的状态和CLActivityType,但不是这里的情况.
任何人都可以帮助我,我做错了什么?
提前致谢.