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

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

问题描述

我想让我的应用程式在背景中持续运作位置服务。为此,我使用了:

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

[locationManager stopUpdatingLocation];
[locationManager startUpdatingLocation];

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

}

但是当我使用 NSTimer 它不调用 UpdateLocation 。我尝试使用另一种方法调用它,但后来它也只调用一次。



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

任何人都可以提出任何建议吗?



提前感谢

解决方案

我在我开发的应用程序中做了这个。当应用程序在后台时,定时器不工作,但应用程序不断接收位置更新。我在文档中的某处(我现在不能找到它,我会发布更新,当我做),一个方法只能在主动运行循环,当应用程序在后台调用。应用程序代理有一个主动的运行循环即使在bg中,所以你不需要创建自己的工作。
[我不知道这是否是正确的解释,但这是我怎么理解从我读了]



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

  CLLocationManager locationManager = [[CLLocationManager alloc] init ]; 

locationManager.delegate = self; //或任何用于管理位置的类

[locationManager startUpdatingLocation];

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

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

int locationUpdateInterval = 300; // 5分钟

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]



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


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];

}

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

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

Can any one please suggest any idea?

Thanks in advance

解决方案

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]

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];

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];
        }
    }
}

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]

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天全站免登陆