react-native-android-background-timer
v1.0.4
Published
Emit event periodically (even when app is in the background or the phone is locked)
Downloads
5
Readme
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
- 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 thegetPackages()
method
- 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')
- 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.
import RNAndroidBackgroundTimer from 'react-native-android-background-timer';
// 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
�<uses-permission android:name="android.permission.WAKE_LOCK" />
� to your manifest.