【问题标题】:Periodic sending small data to server in background mode在后台模式下定期向服务器发送小数据
【发布时间】:2016-04-03 09:27:24
【问题描述】:

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

我该如何执行这项任务?

【问题讨论】:

    标签: ios swift background-process multitasking


    【解决方案1】:

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

    然后在您的 AppDelegate 中导入 LocationTracker.h。

    在您的 didFinishLaunchingWithOptions 中添加以下代码。

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      相关资源
      最近更新 更多