定期在后台模式下向服务器发送小数据 [英] Periodic sending small data to server in background mode

查看:90
本文介绍了定期在后台模式下向服务器发送小数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一种跟踪应用程序,我需要发现一次位置并每10到20秒发送一次(期间值很重要,不能超过). 为了降低电池消耗,我停止了位置更新.这在前台效果很好,但是当应用程序在后台移动时我该怎么办? 我查看了有关后台抓取的信息,但没有确切的时间定期发送数据

I am implementing a kind of tracking app and i need to spot my location once and send it once per 10 - 20 seconds (period value is important and can't be exceeded). To lower battery consumption i stop location updates. This works good in foreground, but how can i do it when app moved in background? I looked info about background fetch, but it hasn't got precise time for periodic sending data

我如何执行此任务?

推荐答案

您可以在应用程序处于后台时启动和停止定期位置更新. 要从位置更新的给定链接中实现此添加类.

You can start and Stop periodic location update while app is in background. To achieve this add class from given link for Location Update.

之后,在您的AppDelegate中导入LocationTracker.h.

After that import LocationTracker.h in your AppDelegate.

在didFinishLaunchingWithOptions中添加以下代码.

Add Below code in your didFinishLaunchingWithOptions.

let locationTracker : LocationTracker  = LocationTracker();
locationTracker?.startLocationTracking();

在LocationTracker.m中,您可以设置重新启动更新的持续时间.在这里,我将其设置为1分钟或60秒.

In LocationTracker.m, you can set duration to restart update.Here i set 1 minute or 60 Seconds.

//Restart the locationMaanger after 1 minute
self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
                                                       selector:@selector(restartLocationUpdates)
                                                       userInfo:nil
                                                        repeats:NO];

您还可以设置获取位置的持续时间.我在这里获取位置10秒钟.

You can also set duration time for fetch Locations. Here I fetch location for 10 Seconds.

self.shareModel.delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self
                                                selector:@selector(stopLocationDelayBy10Seconds)
                                                userInfo:nil
                                                 repeats:NO];

这篇关于定期在后台模式下向服务器发送小数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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