我的服务需要GPS。我实际上实现了一个服务,它打开gps,检查位置是否有效,然后进入睡眠状态一段时间。 (从5秒开始,如果没有检测到移动,它可以睡到一分钟) 之后,我再次启动gps,并获得一个新位置。
但是电池耗尽仍然很高!我使用过locMgr.distanceFilter = 10.0f和desiredAccurady = NearestTenMeters。
如何更多地减少电池消耗?
答案 0 :(得分:1)
这就是我处理它的方式。在我获取位置后,我将其存储在NSDictionary中。然后如果我需要再次定位我返回NSDictionary而不是重新打开GPS。 2分钟后,我重置了NSDictionary(你可以调整你应用程序的最佳套件的时间)。然后,在NSDictionary重置后我下次需要该位置时,我从GPS获取一个新位置。
- (NSDictionary *) getCurrentLocation {
if (self.currentLocationDict == nil) {
self.currentLocationDict = [[NSMutableDictionary alloc] init];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *myLocation = [locationManager location];
[self.currentLocationDict setObject:[NSString stringWithFormat:@"%f", myLocation.coordinate.latitude] forKey:@"lat"];
[self.currentLocationDict setObject:[NSString stringWithFormat:@"%f", myLocation.coordinate.longitude] forKey:@"lng"];
[locationManager stopUpdatingLocation];
[locationManager release];
//Below timer is to help save battery by only getting new location only after 2 min has passed from the last time a new position was taken. Last location is saved in currentLocationDict
[NSTimer scheduledTimerWithTimeInterval:120 target:self selector:@selector(resetCurrentLocation) userInfo:nil repeats:NO];
}
return self.currentLocationDict;
}
- (void) resetCurrentLocation {
NSLog(@"reset");
[currentLocationDict release];
self.currentLocationDict = nil;
}