React Native 后台计时器永不停止 [英] React Native background timer never stops

查看:315
本文介绍了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 后台计时器永不停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆