我的服务需要 GPS。我实际上实现了一个服务,它打开 gps,检查位置是否有效,然后进入休眠状态。 (从 5 秒开始,如果没有检测到运动,它最多可以休眠一分钟) 之后,我再次启动 gps,并获得了一个新位置。
但是电池消耗仍然很高!我使用了 locMgr.distanceFilter = 10.0f 和 desiredAccurady = NearestTenMeters。
如何最大限度地减少电池消耗?
最佳答案
我就是这样处理的。获取位置后,我将其存储在 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;
}
关于iOS GPS电池耗尽,如何减少耗尽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9843836/