如何让 GPS 应用程序在后台运行而不暂停

How to keep GPS app running in background without pauses(如何让 GPS 应用程序在后台运行而不暂停)
本文介绍了如何让 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 应用程序在后台运行而不暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)