繁体   English   中英

如何在反应原生的特定时间安排后台工作

[英]How to schedule background job at specific time in react native

我想在背景中的一天中的特定时间执行一些任务T。 我发现在Android中有可能使用Headless JS 我发现这个库实现了这个https://github.com/vikeri/react-native-background-job并允许你在后台执行东西。

这不完全是我所期待的,它不允许您在特定时间安排任务T. 有谁知道这方面的任何工作?

我已检查此线程执行代码在特定时间反应原生我没有找到我的问题的解决方案。

我遇到了类似的问题,不幸的是,你不能在RN中指定类似于CRON动作的东西。

我对该问题的解决方案是使用此库https://github.com/ocetnik/react-native-background-timer并计算任务计划的当前时间和时间之间的差异。

计算的时间应该以ms为单位,因此您可以将其与提供的函数setTimeout

// Start a timer that runs once after X milliseconds
const timeoutId = BackgroundTimer.setTimeout(() => {
  // this will be executed once after 10 seconds
  // even when app is the the background
  console.log('tac');
}, 10000);

例:

假设你想在明天16日安排任务,在componentDidMount你可以计算从现在到预定日期之间的时间。 让我们用这个moment

componentDidMount(){
  const scheduledDate = 
   moment().add(1,'d').set({hour:16,minute:0,second:0,millisecond:0})
  const diffTime = scheduledDate.diff(moment())
  this.timeoutId = BackgroundTimer.setTimeout(() => {
    console.log('tac');
  }, diffTime);
}

componentWillUnmount(){
 BackgroundTimer.clearTimeout(this.timeoutId);
}

请注意,此解决方案容易受到用户更改手机时间的影响。 完美的解决方案是使用一些外部服务来获取时间。

第二个注意事项,应用程序至少需要在后台才能实现。

JavaScript代码在前台运行,只有一个线程。 如果您需要预定的后台任务,则需要实现本机模块,如RN文档中所述:

https://facebook.github.io/react-native/docs/native-modules-ios.html

https://facebook.github.io/react-native/docs/native-modules-android.html

当然,所有平台限制(尤其是iOS)都适用。

创建类并在您的类中添加它

public static final long NOTIFY_INTERVAL = 10 * 1000; // 30 minutes

@Override
    public void onCreate() {
        // cancel if already existed
        if (mTimer != null) {
            mTimer.cancel();
        } else {
            // recreate new
            mTimer = new Timer();
        }
        // schedule task
        mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
    }

class TimeDisplayTimerTask extends TimerTask {
        @Override
        public void run() {
            // run on another thread
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    // code
                }
            });
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM