|
1 | | -# React Native Background Timer |
2 | | -Emit event periodically (even when app is in the background). |
| 1 | +# @react-native-oh-tpl/react-native-background-timer |
3 | 2 |
|
4 | | -## Installation |
5 | | -1. If you use Expo to create a project [you'll just need to](https://facebook.github.io/react-native/docs/getting-started#caveats) "[eject](https://docs.expo.io/versions/latest/expokit/eject)". |
| 3 | +## 文档地址 / Documentation URL |
6 | 4 |
|
7 | | - ```bash |
8 | | - expo eject |
9 | | - ``` |
| 5 | +[中文 / Chinese](https://gitee.com/react-native-oh-library/usage-docs/blob/master/zh-cn/react-native-background-timer.md) |
10 | 6 |
|
11 | | -2. Install React Native Background Timer package. |
| 7 | +## Codegen |
12 | 8 |
|
13 | | - ```bash |
14 | | - yarn add react-native-background-timer |
15 | | - # or using npm |
16 | | - npm install react-native-background-timer --save |
17 | | - ``` |
| 9 | +该库已接入 codegen,具体请查阅文档。 |
18 | 10 |
|
19 | | -3. Link React Native Background Timer library. This step is not necessary when you use React Native >= 0.60 (and your app is not ejected from Expo). |
| 11 | +## 请悉知 / Acknowledgements |
20 | 12 |
|
21 | | - ```bash |
22 | | - react-native link react-native-background-timer |
23 | | - ``` |
24 | | - |
25 | | -4. If you use CocoaPods or React Native >= 0.60 (and your app is not ejected from Expo) or your app is ejected from Expo, then before running your app on iOS, make sure you have CocoaPods installed and run: |
26 | | - |
27 | | - ```bash |
28 | | - cd ios |
29 | | - pod install |
30 | | - ``` |
31 | | - |
32 | | -Link the library manually if you get errors: |
33 | | - |
34 | | -- Android: `TypeError: Cannot read property 'setTimeout' of undefined` or `TypeError: null is not an object (evaluating 'RNBackgroundTimer.setTimeout')` |
35 | | -- iOS: `Native module cannot be null` |
36 | | - |
37 | | -<details> |
38 | | - <summary>Android manual linking</summary> |
39 | | - |
40 | | -- `android/settings.gradle` |
41 | | - |
42 | | - ```diff |
43 | | - + include ':react-native-background-timer' |
44 | | - + project(':react-native-background-timer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-timer/android') |
45 | | - ``` |
46 | | - |
47 | | -- `android/app/build.gradle` |
48 | | - |
49 | | - ```diff |
50 | | - dependencies { |
51 | | - + implementation project(':react-native-background-timer') |
52 | | - } |
53 | | - ``` |
54 | | - |
55 | | -- `android/app/src/main/java/com/your-app/MainApplication.java` |
56 | | - |
57 | | - ```diff |
58 | | - + import com.ocetnik.timer.BackgroundTimerPackage; |
59 | | -
|
60 | | - @Override |
61 | | - protected List<ReactPackage> getPackages() { |
62 | | - return Arrays.<ReactPackage>asList( |
63 | | - + new BackgroundTimerPackage() |
64 | | - ); |
65 | | - } |
66 | | - ``` |
67 | | -</details> |
68 | | - |
69 | | -<details> |
70 | | - <summary>iOS manual linking</summary> |
71 | | - |
72 | | -- `ios/Podfile` |
73 | | - |
74 | | - ```diff |
75 | | - + pod 'react-native-background-timer', :path => '../node_modules/react-native-background-timer' |
76 | | - ``` |
77 | | -</details> |
78 | | - |
79 | | -## Usage |
80 | | - |
81 | | -```js |
82 | | -import BackgroundTimer from 'react-native-background-timer'; |
83 | | -``` |
84 | | - |
85 | | -### Crossplatform |
86 | | -To use the same code both on Android and iOS use runBackgroundTimer() and stopBackgroundTimer(). There can be used only one background timer to keep code consistent. |
87 | | - |
88 | | -```js |
89 | | -BackgroundTimer.runBackgroundTimer(() => { |
90 | | -//code that will be called every 3 seconds |
91 | | -}, |
92 | | -3000); |
93 | | -//rest of code will be performing for iOS on background too |
94 | | -
|
95 | | -BackgroundTimer.stopBackgroundTimer(); //after this call all code on background stop run. |
96 | | -``` |
97 | | - |
98 | | -### iOS |
99 | | -After iOS update logic of background task little bit changed. So we can't use as it was. |
100 | | -You have to use only start() and stop() without parameters. And all code that is performing will continue performing on background including all setTimeout() timers. |
101 | | -
|
102 | | -Example: |
103 | | -```js |
104 | | -BackgroundTimer.start(); |
105 | | -// Do whatever you want incuding setTimeout; |
106 | | -BackgroundTimer.stop(); |
107 | | -``` |
108 | | -
|
109 | | -> If you call stop() on background no new tasks will be started! |
110 | | -> Don't call .start() twice, as it stop performing previous background task and starts new. |
111 | | -> If it will be called on backgound no tasks will run. |
112 | | - |
113 | | -### Android |
114 | | -You can use the `setInterval` and `setTimeout` functions. |
115 | | -This API is identical to that of `react-native` and can be used to quickly replace existing timers |
116 | | -with background timers. |
117 | | - |
118 | | -```js |
119 | | -// Start a timer that runs continuous after X milliseconds |
120 | | -const intervalId = BackgroundTimer.setInterval(() => { |
121 | | - // this will be executed every 200 ms |
122 | | - // even when app is the the background |
123 | | - console.log('tic'); |
124 | | -}, 200); |
125 | | -
|
126 | | -// Cancel the timer when you are done with it |
127 | | -BackgroundTimer.clearInterval(intervalId); |
128 | | -``` |
129 | | - |
130 | | -```js |
131 | | -// Start a timer that runs once after X milliseconds |
132 | | -const timeoutId = BackgroundTimer.setTimeout(() => { |
133 | | - // this will be executed once after 10 seconds |
134 | | - // even when app is the the background |
135 | | - console.log('tac'); |
136 | | -}, 10000); |
137 | | -
|
138 | | -// Cancel the timeout if necessary |
139 | | -BackgroundTimer.clearTimeout(timeoutId); |
140 | | -``` |
141 | | -
|
142 | | -### Obsolete |
143 | | -Obsolete usage which doesn't support multiple background timers. |
144 | | -
|
145 | | -```js |
146 | | -import { |
147 | | - DeviceEventEmitter, |
148 | | - NativeAppEventEmitter, |
149 | | - Platform, |
150 | | -} from 'react-native'; |
151 | | -
|
152 | | -import BackgroundTimer from 'react-native-background-timer'; |
153 | | -``` |
154 | | -
|
155 | | -```js |
156 | | -const EventEmitter = Platform.select({ |
157 | | - ios: () => NativeAppEventEmitter, |
158 | | - android: () => DeviceEventEmitter, |
159 | | -})(); |
160 | | -``` |
161 | | -
|
162 | | -```js |
163 | | -// start a global timer |
164 | | -BackgroundTimer.start(5000); // delay in milliseconds only for Android |
165 | | -``` |
166 | | -```js |
167 | | -// listen for event |
168 | | -EventEmitter.addListener('backgroundTimer', () => { |
169 | | - // this will be executed once after 5 seconds |
170 | | - console.log('toe'); |
171 | | -}); |
172 | | -``` |
173 | | -```js |
174 | | -// stop the timer |
175 | | -BackgroundTimer.stop(); |
176 | | -``` |
| 13 | +本项目基于 [The MIT License (MIT)](https://github.com/react-native-oh-library/react-native-background-timer/blob/sig/LICENSE) ,请自由地享受和参与开源。 |
0 commit comments