反应原生背景计时器
定期发布事件(即使应用在背景中).
安装
-
如果您使用 Expo 创建工程 你会只需要 " (中文(简体) )弹出" ..
expo eject -
安装本地背景计时器包 .
yarn add @boterop/react-native-background-timer # or using npm npm install @boterop/react-native-background-timer --save -
Link React 原生背景计时器库. 当您使用 React Industrial QQ 0.60(且您的应用程序没有被从 Expo 中弹出)时,此步骤没有必要.
react-native link @boterop/react-native-background-timer -
如果您使用 CocoaPods 或 React Introduct 0.60 (且您的应用程序没有被从 Expo 中弹出) 或您的应用程序被从 Expo 中弹出, 那么在运行您的应用程序在 iOS 上之前, 请确保您安装并运行 CocoaPods :
cd ios pod install
错误时手动链接库 :
- 机器人:
TypeError: Cannot read property 'setTimeout' of undefined或者说TypeError: null is not an object (evaluating 'RNBackgroundTimer.setTimeout') - iOS: (英语)
Native module cannot be null
Android 手册链接
-
android/settings.gradle+ include ':react-native-background-timer' + project(':react-native-background-timer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-timer/android') -
android/app/build.gradledependencies { + implementation project(':react-native-background-timer') } -
android/app/src/main/java/com/your-app/MainApplication.java```diff + import io.boterop.timer.BackgroundTimerPackage; @Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( + new BackgroundTimerPackage() ); } ```
iOS 手册链接
-
ios/Podfile```diff + pod '@boterop/react-native-background-timer', :path => '../node_modules/@boterop/react-native-background-timer' ```
使用量
import BackgroundTimer from '@boterop/react-native-background-timer';
跨平台
要在Android和iOS上使用相同的代码,则使用 runBackgroundTimer ()和 stopBackgroundTimer (). 只能使用一个背景计时器来保持代码的一致性.
BackgroundTimer.runBackgroundTimer(() => {
//code that will be called every 3 seconds
}, 3000);
//rest of code will be performing for iOS on background too
BackgroundTimer.stopBackgroundTimer(); //after this call all code on background stop run.
iOS系统
在iOS更新背景任务的逻辑稍有改变后. 所以我们不能像以前那样使用. 您只需使用没有参数的起始( ) 和停止( ) 。 而所有正在执行的代码将继续在背景上执行,包括所有设置的Timeout()定时器.
示例
BackgroundTimer.start();
// Do whatever you want incuding setTimeout;
BackgroundTimer.stop();
如果您在背景上呼叫停止( ) , 将不会启动新任务 ! 不要两次拨打.start(),因为它停止执行之前的背景任务并开始新的任务. 如果用后台调用的话, 任务就不会完成 .
机器人
你可以用这个 setInterval 财务报告和审定财务报表 setTimeout 函数.
此API与 react-native 并可用于快速替换已存在的定时器
带有背景计时器.
// 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);
// Cancel the timer when you are done with it
BackgroundTimer.clearInterval(intervalId);
// 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);
// Cancel the timeout if necessary
BackgroundTimer.clearTimeout(timeoutId);
过时
过时的使用不支持多个背景计时器.
import {
DeviceEventEmitter,
NativeAppEventEmitter,
Platform,
} from 'react-native';
import BackgroundTimer from '@boterop/react-native-background-timer';
const EventEmitter = Platform.select({
ios: () => NativeAppEventEmitter,
android: () => DeviceEventEmitter,
})();
// start a global timer
BackgroundTimer.start(5000); // delay in milliseconds only for Android
// listen for event
EventEmitter.addListener('backgroundTimer', () => {
// this will be executed once after 5 seconds
console.log('toe');
});
// stop the timer
BackgroundTimer.stop();