如何让GPS应用程序在没有停顿的情况下在后台运行 [英] How to keep GPS app running in background without pauses
问题描述
我在保持GPS在后台运行时遇到问题。
当应用程序处于后台时,我成功保持GPS运行,但有时会停止。我注意到,当我进入建筑物时停下来,我在那里呆了2-3个小时。 停止意味着即使位置服务图标仍然打开,我仍未收到任何GPS点。然后当我离开时,我没有收到任何积分,直到我将应用程序带到前台。
我将pausesLocationUpdatesAutomatically属性设置为NO。
所以,一切工作正常,直到我进入一个弱gps信号的位置。当我离开那个区域时,我不再收到积分,即使我应该因为gps信号现在好。
这里是我的代码,当应用程序进入后台:
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog @[AppDelegate]:应用程序进入后台!);
if(doesUserWantGPSTracking)
{
[self stopGPS];
UIApplication * app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:
^ {
dispatch_async(dispatch_get_main_queue(),
^ {
if bgTask!= UIBackgroundTaskInvalid)
{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}];
$ b dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),
^ {
[self startGPS];
dispatch_async(dispatch_get_main_queue(), ^ b
bgTask = UIBackgroundTaskInvalid
$
});
});
}
}
您的信息plist,添加所需的背景模式
并将一个项目设置为应用程序寄存器位置更新
更新:
随着ios返回到 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
当距离发生显着变化时。我想你需要改变:
//设置移动阈值。
locationManager.distanceFilter = 500; //米
I have a problem keeping the GPS running in background.
I succeded to keep the GPS running when the app is in background, but it stops sometimes. I noticed that it stops when i enter in a building and i stay there for 2-3 hours. "Stops" means that i don't receive any GPS point even though the location service icon is still on. Then when i get out i don't receive any points until I bring my application to foreground.
I set the pausesLocationUpdatesAutomatically property to NO.
So, everything works fine until i get into a location with weak gps signal. When i get out of that area i don't receive points anymore, even though I should because the gps signal is now good.
And here is my code when the applications enters in background:
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(@"[AppDelegate]: Application entering in background!");
if(doesUserWantGPSTracking)
{
[self stopGPS];
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:
^{
dispatch_async(dispatch_get_main_queue(),
^{
if( bgTask != UIBackgroundTaskInvalid )
{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
[self startGPS];
dispatch_async(dispatch_get_main_queue(), ^{
if( bgTask != UIBackgroundTaskInvalid )
{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
});
}
}
In your info plist, add Required background modes
and set one item to App registers for location updates
Updated:
As ios return to - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
when there is a significant distance change. I guess you need to change:
// Set the movement threshold.
locationManager.distanceFilter = 500; // meters
这篇关于如何让GPS应用程序在没有停顿的情况下在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!