React Native - 屏幕关闭时的计时器进度

React Native - timer progress with screen off

我需要在计时器结束时响铃。 我是 运行 一个 setInterval() 的计时器。屏幕打开时它工作正常,并且该应用程序阻止屏幕自动关闭。但是,如果用户使用电源按钮关闭屏幕,定时器将在 3 分钟后停止计时,并且不会触发声音。这是由于 Android 睡眠模式和没有唤醒锁,我假设。如果我再次打开屏幕,它会继续滴答作响。如何启用唤醒锁?

我试过:

这是我的计时器中的一个(有效的)示例函数代码class:

play = () => {
    if (this.timeLeft != 0) {
      this.paused = false
      this.running = BackgroundTimer.setInterval(() => {
        if (!this.paused) {
          this.timePassed++
          this.timeLeft = this.duration - this.timePassed     
        }
        if(this.timeLeft <= 0) {
          BackgroundTimer.clearInterval(this.running)
          this.playBell()
        }
      }, 1000)
    } else {
      this.stopped = true
    }
  }

如何确保代码在唤醒锁的情况下运行?

不想弄乱本机代码,我做了以下操作:

react-native-video 支持 playInBackground={true} 的屏幕关闭播放,onProgressonEnd 等在屏幕关闭时继续工作,所以我 运行 这些回调中的所有计时器滴答代码。如果你没有使用定时器来播放 video/audio,你仍然可以通过重复播放一个小的无声音频文件来使用 react-native-video。