【问题标题】:Using of Set Interval in React Native Android and iOS在 React Native Android 和 iOS 中使用 Set Interval
【发布时间】:2019-07-29 02:34:39
【问题描述】:

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

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

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

如果我使用 headlessJs,React Native 运行计时器不是很痛苦吗?


有一些想要的库,

react-native-background-job - 使用 headlessJs 并且仅在 android 中工作。

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

react-native-background-fetch

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

为什么只有音乐播放器在后台工作?

【问题讨论】:

    标签: javascript react-native


    【解决方案1】:

    我从 react-native-background-timer 库中找到了解决方案。

    在 iOS 中,我们可以在执行以下代码后启动计时器或在后台做任何反应原生的东西。

    即使我们在设备中使用其他应用程序,后台计时器也能正常工作。 (例如:iTunes、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);
    

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

    【讨论】:

      猜你喜欢
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      相关资源
      最近更新 更多