简体   繁体   English

iOS后台应用程序:运行应用程序的最佳方法

[英]iOS Background app : best way to run app

I need to create an app that retrieves iOS radio informations (rssi, bearer, ...) in background mode ; 我需要创建一个在后台模式下检索iOS广播信息(rssi,bearer等)的应用程序; the app will be available on app store and thereby is under App Store guidelines. 该应用程序将在应用程序商店中提供,并因此符合应用程序商店指南。 The app should rationally keep the phone's battery. 该应用程序应合理保存手机的电池。

I currently use BackgroundFetch and GPS together, it works pretty good but when iOS has not enough memory the app is killed by OS. 我目前将BackgroundFetch和GPS一起使用,效果很好,但是当iOS内存不足时,该应用将被OS杀死。

I also looked Google app (Google Now) that uses GPS and keep iPhone's battery life, but i don't know how they do that. 我还看过使用GPS并保持iPhone电池寿命的Google应用(Google Now),但我不知道他们是怎么做到的。

Thanks 谢谢

  1. Make sure "Background Fetch" and "Location Updates" are enabled in your Plist. 确保在您的Plist中启用了“后台获取”和“位置更新”。
  2. When your app is in the background, user startMonitoringSignificantLocationChanges instead of startUpdatingLocation. 当您的应用程序在后台运行时,用户startMonitoringSignificantLocationChanges而不是startUpdatingLocation。 Here's some info from the documentation. 这是文档中的一些信息。

"[startUpdatingLocation] typically require the location-tracking hardware to be enabled for longer periods of time, which can result in higher power usage." “ [startUpdatingLocation]通常要求在更长的时间内启用位置跟踪硬件,这可能导致更高的功耗。” "[With startMonitoringSignificantLocationChanges] Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes." “ [with startMonitoringSignificantLocationChanges]应用程序可以在设备距其先前的通知移动500米或更远时立即收到通知。它不应每隔五分钟收到一次通知。

I'd go through the documentation here so you can grab a better understanding ( https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instm/CLLocationManager/startMonitoringSignificantLocationChanges ) 我将在这里浏览文档,以便您可以更好地理解( https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/ instm / CLLocationManager / startMonitoringSignificantLocationChanges

As you can probably see, using startMonitoringSignificantLocationChanges significantly increases battery life. 您可能会看到,使用startMonitoringSignificantLocationChanges可以大大延长电池寿命。 Here's how I implemented it in my AppDelegate.m: 这是我在AppDelegate.m中实现它的方式:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    [self.locationManager startMonitoringSignificantLocationChanges];
    ...
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    ...
    [self.locationManager stopUpdatingLocation];
    [self.locationManager startMonitoringSignificantLocationChanges];
    ...
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    ...
    [self.locationManager stopMonitoringSignificantLocationChanges];
    [self.locationManager startUpdatingLocation];
    ...
}

-(CLLocationManager *)locationManager{

    if(!_locationManager){
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        _locationManager.distanceFilter = 5;
    }
    return _locationManager;
}

This way I ensure that I only use the power-draining "startUpdatingLocation" method when the app is active and the power-saving "stopMonitoringSignificantLocationChanges" when inactive. 这样,我可以确保仅在应用程序处于活动状态时才使用耗电的“ startUpdatingLocation”方法,而在不活动状态时仅使用省电的“ stopMonitoringSignificantLocationChanges”。

I made on test on your code above. 我对上面的代码进行了测试。

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    //NSLog(@"Location: %f, %f", newLocation.coordinates.longtitude, newLocation.coordinates.latitude);
    longitude = newLocation.coordinate.longitude;
    latitude = newLocation.coordinate.latitude;
    gpsTimestamp = [newLocation.timestamp timeIntervalSince1970];
    NSLog(@"Changed ! ");
    if (!timer &&  ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)) {
        NSLog(@"Starting task");
        timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(createBgIndicator) userInfo:nil repeats:YES];

    }

}

In this case, the timer will start only while I'm moving and is executed 5 times (10s). 在这种情况下,计时器将仅在我移动时启动,并执行5次(10秒)。 I would like to get the timer running all the time. 我想让计时器一直运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM