如何让 GPS 应用程序在后台运行而不暂停 [英] How to keep GPS app running in background without pauses

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

问题描述

让 GPS 在后台运行时遇到问题.

I have a problem keeping the GPS running in background.

当应用程序处于后台时,我成功地保持 GPS 运行,但有时会停止.我注意到当我进入建筑物时它会停止并在那里停留 2-3 个小时.停止"意味着即使定位服务图标仍然亮着,我也没有收到任何 GPS 点.然后,当我离开时,直到我把我的应用程序带到前台,我才会收到任何积分.

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.

我将 pausesLocationUpdatesAutomatically 属性设置为 NO.

I set the pausesLocationUpdatesAutomatically property to NO.

所以,在我进入 GPS 信号较弱的位置之前,一切正常.当我离开那个区域时,我不再获得积分,即使我应该获得积分,因为现在 gps 信号很好.

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

更新:

当 ios 返回 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 时,距离发生重大变化.我猜你需要改变:

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屋!

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