定期在后台模式下向服务器发送小数据

编程入门 行业动态 更新时间:2024-10-24 12:22:55
本文介绍了定期在后台模式下向服务器发送小数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在实现一种跟踪应用程序,我需要发现一次位置并每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];

更多推荐

定期在后台模式下向服务器发送小数据

本文发布于:2023-11-27 09:00:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   服务器   数据   模式下

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!