在后台持续运行应用程序 [英] to run app continuously in the background

查看:120
本文介绍了在后台持续运行应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用能够继续在后台运行定位服务。为此,我使用了:

I want to my app to keep running in the background with location services. For this I have used:

-(void)applicationDidEnterBackground:(UIApplication *)application {

    [locationManager stopUpdatingLocation];
    [locationManager startUpdatingLocation];

    //timer=[NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(UpdateLocation) userInfo:nil repeats:YES];

}

但是当我使用 NSTimer 它不会调用 UpdateLocation 。我尝试使用其他方法调用它,但之后它也只调用一次。

but when I use NSTimer it does not call UpdateLocation. I tried calling it using another method but then it also called only once.

我想在后台连续运行应用程序,在定期间隔后检测位置。 / p>

I want to run the app continuously in the background, detecting locations after regular interval of time.

推荐答案

我在我正在开发的应用程序中这样做了。当应用程序在后台但应用程序不断收到位置更新时,计时器不起作用。我在文档中的某处读过(我现在似乎无法找到它,我会在发布更新时),当应用程序在后台时,只能在活动的运行循环上调用方法。即使在bg中,app委托也有一个活动的运行循环,所以你不需要创建自己的循环来使这个工作。
[我不确定这是不是正确的解释,但这就是我从我读到的内容中理解的]

I did this in an application I'm developing. The timers don't work when the app is in the background but the app is constantly receiving the location updates. I read somewhere in the documentation (i can't seem to find it now, i'll post an update when i do) that a method can be called only on an active run loop when the app is in the background. The app delegate has an active run loop even in the bg so you dont need to create your own to make this work. [Im not sure if this is the correct explanation but thats how I understood from what i read]

首先,添加'location'对象为应用程序的info.plist中的关键'背景模式'。现在,您需要做的是在应用中的任何位置启动位置更新:

First of all, add the 'location' object for the key 'Background Modes' in your app's info.plist. Now, what you need to do is start the location updates anywhere in your app:

CLLocationManager locationManager = [[CLLocationManager alloc] init];

locationManager.delegate = self;//or whatever class you have for managing location

[locationManager startUpdatingLocation];

接下来,写一个方法来处理位置更新,
说 - (void)didUpdateToLocation :(CLLocation *)位置,在app委托中。然后在启动位置管理器的类中实现方法locationManager:didUpdateLocation:fromLocation of CLLocationManagerDelegate(因为我们将位置管理器委托设置为'self')。在此方法中,您需要检查是否已经过了必须处理位置更新的时间间隔。您可以通过每次保存当前时间来完成此操作。如果该时间已过,请从您的应用代理调用方法UpdateLocation:

Next, write a method to handle the location updates, say -(void)didUpdateToLocation:(CLLocation*)location, in the app delegate. Then implement the method locationManager:didUpdateLocation:fromLocation of CLLocationManagerDelegate in the class in which you started the location manager (since we set the location manager delegate to 'self'). Inside this method you need to check if the time interval after which you have to handle the location updates has elapsed. You can do this by saving the current time every time. If that time has elapsed, call the method UpdateLocation from your app delegate:

NSDate *newLocationTimestamp = newLocation.timestamp;
NSDate *lastLocationUpdateTiemstamp;

int locationUpdateInterval = 300;//5 mins

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (userDefaults) {

        lastLocationUpdateTiemstamp = [userDefaults objectForKey:kLastLocationUpdateTimestamp];

        if (!([newLocationTimestamp timeIntervalSinceDate:lastLocationUpdateTiemstamp] < locationUpdateInterval)) {
            //NSLog(@"New Location: %@", newLocation);
            [(AppDelegate*)[UIApplication sharedApplication].delegate didUpdateToLocation:newLocation];
            [userDefaults setObject:newLocationTimestamp forKey:kLastLocationUpdateTimestamp];
        }
    }
}

这将每次调用你的方法即使您的应用处于后台也需要5分钟。
Imp:如果您的位置数据的准确性不是很严重,这个实现会消耗电池,您应该使用[locationManager startMonitoringSignificantLocationChanges]

This will call your method every 5 mins even when your app is in background. Imp: This implementation drains the battery, if your location data's accuracy is not critical you should use [locationManager startMonitoringSignificantLocationChanges]

在将此添加到您的应用之前,请阅读
的位置感知编程指南 http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html

Before adding this to your app, please read the Location Awareness Programming Guide at http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html

这篇关于在后台持续运行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆