react-native-android-background-timer
Version:
Emit event periodically (even when app is in the background or the phone is locked)
56 lines (40 loc) • 1.88 kB
Markdown
# react-native-android-background-timer
Emit event periodically (even when app is in the background or the phone is locked).
## Getting started
`$ npm install react-native-android-background-timer --save`
### Mostly automatic installation
`$ react-native link react-native-android-background-timer`
### Manual installation
#### Android
1. Open up `android/app/src/main/java/[...]/MainActivity.java`
- Add `import com.reactlibrary.RNAndroidBackgroundTimerPackage;` to the imports at the top of the file
- Add `new RNAndroidBackgroundTimerPackage()` to the list returned by the `getPackages()` method
2. Append the following lines to `android/settings.gradle`:
```
include ':react-native-android-background-timer'
project(':react-native-android-background-timer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-background-timer/android')
```
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
```
compile project(':react-native-android-background-timer')
```
## Usage
You can use the setInterval and setTimeout functions. This API is identical to that of react-native and can be used to quickly replace existing timers with background timers.
```javascript
import RNAndroidBackgroundTimer from 'react-native-android-background-timer';
```
```javascript
// Start a timer that runs continuous after X milliseconds
const intervalId = RNAndroidBackgroundTimer.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
RNAndroidBackgroundTimer.clearInterval(intervalId);
```
Don't forget to add
```xml
�<uses-permission android:name="android.permission.WAKE_LOCK" />
```
� to your manifest.