iOS GPS电池耗尽,如何减少耗尽?

标签 ios

我的服务需要 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/

相关文章:

ios - 头脑 Storm Objective-C 运行时

ios - 使用 CATransform3D 旋转 UIImage 并仅获得裁剪后的新 UIImage

ios - 如何使用 SpriteKit 和 Swift 打开 url

ios - 在 tabViewController setSelectedIndex 之后旋转不为 View 设置动画

ios - 如何快速触发动画序列

ios - Swift 如何在不中断 reloadData 的情况下显示视频

ios - 使用 Swift 从特定网页检索和解析文本

带有改变大小的 UIButtons 的 iOS 回流布局

ios - UITableView 在 scrollViewDidEndDragging 时滚动到特定点

Javascript 音频无法在移动设备上运行