Android:如何启动一个计时器,在下一个精确的2秒倍数开始?

huangapple go评论96阅读模式
英文:

Android: How can I start a timer that begins at the next exact multiple of 2 seconds?

问题

我是新手安卓开发者,被投入到一个相当复杂的项目中。

我有多台运行相同应用的设备,它们要么是员工设备,要么是排除区域设备。

我需要使用蓝牙信标让这些设备传输它们的GPS坐标,而排除区域设备还要传输它们的边界大小(蓝牙信标距离检测不够可靠)。

我想要创建一个运行的服务(我认为),让员工设备在一秒钟内监听,而排除区域设备广播,然后所有设备在接下来的一秒钟内切换监听和广播,然后重复。

所有被检测到的边界违规情况都要通过计算设备自身GPS坐标与对立类型设备广播的坐标之间的距离来记录。

如何启动一个计时器,并每2秒钟重复调用该函数,精确到2秒的下一个整数倍?不是从调用函数的时间点开始的2秒。例如,如果时间是12:35:01:223,我希望重复调用函数的时间是12:35:02。

英文:

I am new to android and have been thrown in at the deep end with a somewhat complicated project.

I effectively have a number of devices running the same app and they are either staff or Exclusion zones.
I need to use bluetooth beacons to have the devices transmit their GPS coordinates, and the exclusion zones also transmit their boundary size (Bluetooth beacon distance detection was not reliably accurate enough.)

What I want to do is have a service running (I think) that has the staff devices listen for one second, whilst the exclusion zones broadcast, then all devices flip between listening and broadcasting for the next second, then repeat.
Any detected boundary violations calculated by taking the distance between the devices own GPS coordinate and any that have been broadcast by other devices of the opposing type are then to be logged.

How can I get a timer to start, and repeatedly call the function every 2 seconds, at the next exact multiple of 2 seconds?

  • NOT 2 seconds from the point at which the function is called. For instance if the time is 12h:35m:01s:223ms I want the repeating function to be called at 12:35:02

答案1

得分: 1

计算距离下一个两秒标记还剩多少时间,然后使用该时间来安排一个延迟的、一次性任务,该任务将启动一个每两秒重复一次的计时器。

英文:

Compute the time remaining to the next two-second mark and then use that to schedule a delayed, one-time task that will start a two-second repeating timer.

huangapple
  • 本文由 发表于 2020年8月14日 00:58:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63399879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定