React Native 后台计时器永不停止

编程入门 行业动态 更新时间:2024-10-28 18:30:49
本文介绍了React Native 后台计时器永不停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在构建一个应用程序,它有一个计时器来在计时器处于活动状态时请求地理定位.对于计时器,我正在使用 react-native-background-timer.这是一种工作,但不完全是我想要的.

I'm building an app that has a timer to request geolocation while the timer is active. For a timer I'm using react-native-background-timer. Which is kind of working, but not exactly as I want.

用这段代码:

BackgroundTimer.runBackgroundTimer(() => { console.log('tic'); }, 1000);

当应用程序在后台时,计时器正在停止.这段代码:

the timer is stopping when the app is in the background. While with this piece of code:

const intervalId = BackgroundTimer.setInterval(() => { console.log('tic'); }, 1000);

它即使在后台也一直在运行,但我无法阻止它.当我运行 BackgroundTimer.clearInterval(intervalId); 时,计时器仍在运行.即使当我离开屏幕并返回主屏幕时,计时器仍然滴答作响,永不停止.这并不理想,因为我需要计时器运行几分钟然后停止.

it runs constantly even in the background, but I'm not able to stop it. When I run BackgroundTimer.clearInterval(intervalId);, the timer still runs. Even when I leave the screen and go back to my Home screen, the timer still ticks and never stops. This is not ideal, because I need timer to run for a few minutes and then stop.

我将计时器设置为 1 秒以更新屏幕上剩余的时间.我正在考虑将计时器设置为 6 分钟一次,但是如何每秒更新状态?为这个制作 2 个计时器感觉是一种糟糕的做法,尽管它可以工作.

I set the timer to 1 second to update time left on the screen. I was thinking about setting a timer once for 6 minutes, but then how do I update the state every second? Making 2 timers for this feels like a bad practise, even though it would work.

所以为了更清楚,用户假设进行某些活动,例如步行几分钟.因此,当用户接听电话或打开音乐应用程序切换音乐或其他内容时,我不能让计时器停止.计时器仍然需要运行,我需要通过地理位置测量步数和距离.即使用户打开另一个应用程序,忘记我的应用程序,它也需要完美运行,并且它仍然会运行剩余的时间,然后在数据库中记录并停止.

So to make it more clear, the user suppose to engage in certain activity like walking for a few minutes. So I can't let the timer to stop when user is answering a call or opened a music app to switch music or something else. Timer still needs to run and I need to measure the number of steps and distance with geolocation. It need to work flawlessly even if user opened another app, forgot about my app, and it would still run for the remaining time, then made a record to the database and stopped.

推荐答案

试试下面的代码片段,适用于安卓和ios

Try the following code snippet, works both on android and ios

import { DeviceEventEmitter, NativeAppEventEmitter, Platform } from 'react-native'; import _BackgroundTimer from 'react-native-background-timer'; const EventEmitter = Platform.select({ ios: () => NativeAppEventEmitter, android: () => DeviceEventEmitter, })(); class BackgroundTimer { static setInterval(callback, delay) { _BackgroundTimer.start(); this.backgroundListener = EventEmitter.addListener("backgroundTimer", () => { this.backgroundTimer = _BackgroundTimer.setInterval(callback, delay); }); return this.backgroundListener; } static clearInterval(timer) { if (timer) timer.remove(); if (this.backgroundTimer) _BackgroundTimer.clearInterval(this.backgroundTimer); _BackgroundTimer.stop(); } } export default BackgroundTimer;

使用

const timer = BackgroundTimer.setInterval(callback, 1000); BackgroundTimer.clearInterval(timer)

更多推荐

React Native 后台计时器永不停止

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

发布评论

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

>www.elefans.com

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