簡體   English   中英

在后台持續運行應用程序

[英]to run app continuously in the background

我想讓我的應用程序繼續在后台運行位置服務。 為此我用過:

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

    [locationManager stopUpdatingLocation];
    [locationManager startUpdatingLocation];

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

}

但是當我使用NSTimer它不會調用UpdateLocation 我嘗試使用另一種方法調用它,但之后它也只調用一次。

我想在后台持續運行應用程序,在定期間隔后檢測位置。

我在我正在開發的應用程序中這樣做了。 當應用程序在后台但應用程序不斷收到位置更新時,計時器不起作用。 我在文檔中的某處讀過(我現在似乎無法找到它,當我這樣做時會發布更新),當應用程序在后台時,只能在活動的運行循環上調用方法。 即使在bg中,app委托也有一個活躍的運行循環,所以你不需要創建自己的循環來使這個工作。 [我不確定這是否是正確的解釋,但這就是我從我所讀到的內容中理解的]

首先,在應用程序的info.plist中為“背景模式”鍵添加“位置”對象。 現在,您需要做的是在應用中的任何位置啟動位置更新:

CLLocationManager locationManager = [[CLLocationManager alloc] init];

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

[locationManager startUpdatingLocation];

接下來,編寫一個方法來處理位置更新,比如 - (void)didUpdateToLocation:(CLLocation *)位置,在app delegate中。 然后在啟動位置管理器的類中實現方法locationManager:didUpdateLocation:fromLocation of CLLocationManagerDelegate(因為我們將位置管理器委托設置為'self')。 在此方法中,您需要檢查是否已經過了必須處理位置更新的時間間隔。 您可以通過每次保存當前時間來完成此操作。 如果該時間已過,請從您的應用委托調用方法UpdateLocation:

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]

在將此添加到您的應用程序之前,請閱讀http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html上的位置感知編程指南。

我知道答案已經很晚了。 但如果你仍然沒有得到答案,這就是我所做的,我的應用程序在后台持續運行。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
     UIApplication *app = [UIApplication sharedApplication];
     UIBackgroundTaskIdentifier bgTask = 0;

     backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self  selector:@selector(backgroundTask) userInfo:nil repeats:YES];

     bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
    }];
}

現在在backgroundTask方法中做任何你想做的事。

您可以在后台運行應用程序以執行特定任務。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];
    UIBackgroundTaskIdentifier bgTask = 0;

    backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(backgroundTask) userInfo:nil repeats:YES];

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
    }];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM