在 React Native Android 和 iOS 中使用 Set Interval [英] Using of Set Interval in React Native Android and iOS

查看:39
本文介绍了在 React Native Android 和 iOS 中使用 Set Interval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

React Native Video 和计时器(使用 setInterval)都是在前台工作,当应用程序在后台时停止计时器.但视频仍在后台播放.

Both React Native Video and timer (used setInterval) are worked on foreground and timer is stopped when app is in the background. But still video plays in the background.

这意味着,当应用程序暂停时,所有 setInterval 的运行和(以及 setTimeouts 挂起)也会暂停.

It means, when the application pauses, so do all setInterval's running and (and setTimeouts pending).

headlessJs api 仅适用于 android 在后台运行任务.

headlessJs api is available only for android to run the task in background.

如果我使用 headlessJs,对于 react native 运行计时器来说,产品不是很痛苦吗?

If I use headlessJs, Isn't product pain for react native to run a timer?

<小时>

有一些需要的库,

react-native-background-job - 使用 headlessJs 并且只工作在安卓中.

react-native-background-job - Use headlessJs and work only in android.

react-native-background-task - 在 android 和ios 使用了一个围绕 react-native-background-fetch 库的代理(仅支持单个任务,任务执行的确切时间不可预测)

react-native-background-task - Use headlessJs in android and ios uses a proxy around react-native-background-fetch library (Support only for a single task,The exact timings of task execution are unpredictable)

react-native-background-fetch

如果我使用那些库,我可以在后台服务中使用 setInterval 吗?

Can I use setInterval in the background service if I use those libraries?

为什么只有音乐播放器在后台运行?

Why only music player is working in the background?

推荐答案

我从 react-native-background-timer 库.

在 iOS 中,我们可以在执行以下代码后启动计时器或在后台执行任何本地反应.

In iOS, we can start the timer or do any react native stuff in the background after execute the following code.

即使我们在设备中使用其他应用,后台计时器也能正常工作.(例如:itunes,whatsup)

Background timer works even we use other apps in the device. (Ex: itune, whatsup)

// Import the library
import BackgroundTimer from 'react-native-background-timer';

// Start the timer
BackgroundTimer.start();

// Start a timer that runs continuous after X milliseconds
const intervalId = BackgroundTimer.setInterval(() => {

// this will be executed every 200 ms
// even when app is the the background
console.log('tic');

}, 200);

在安卓中,

// Import the library
import BackgroundTimer from 'react-native-background-timer';

// Start a timer that runs continuous after X milliseconds
const intervalId = BackgroundTimer.setInterval(() => {

   // this will be executed every 200 ms
   // even when app is the the background
   console.log('tic');
}, 200);

注意:用设备测试,不要相信模拟器

Note: Test with the device and don't trust the simulator

这篇关于在 React Native Android 和 iOS 中使用 Set Interval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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