Android: BroadcastReceiver N秒内没有OnReceive触发时的调用方法

Android: Call Method when BroadcastReceiver hasn't had OnReceive trigger for N seconds

我正在开发一个需要 constant/accurate 位置更新的应用程序(用户正在评估基础设施并将拍摄标记到其位置的损坏照片)。因为我需要在多个活动中使用 locationlistener 运行,所以我在此处关注 this answer 并使用广播接收器来获取更新。

然而,我意识到对于如何检查 GPS 信号是否丢失,并没有真正达成共识。我注意到当我移动 indoors/lose 信号时 onReceive() 停止激活。

有什么方法可以检查 OnReceive() 函数是否在最后 N 秒内触发失败?理想情况下,我可以将我的 locationlistener 设置为 1 Hz(每秒 1 次)的更新频率,因此如果我超过 10 秒没有更新,我可以向用户指示他们不再接收更新。

Since I need to have the locationlistener running across multiple activities I followed this answer here and used a broadcast reciever to get the updates.

您不一定需要使用 BroadcastReceiverLocationListener 方法可用于多个活动。您只需要监听器和管理它的东西在任何 activity 之外(例如,某种存储库,通过 LiveData 向您的视图模型提供数据)。

Is there a way I can check if the OnRecieve() function has failed to trigger in the last N seconds?

第 1 步:注册位置更新时启动 N 秒计时器,例如在 ScheduledExecutorService 上使用 schedule(),或从 RxJava

使用 Single.timer() ]

第 2 步:当 onReceive() 被调用时,取消现有计时器(例如,cancel()ScheduledFuture 上,您从 schedule() 获得)并安排一个新的N秒计时器

步骤 #3:如果您从计时器获得控制权(例如,调用提供给 schedule()CallableRunnable),您知道 N 秒过去了而没有被调用 onReceive()