rn-android-background-location
v1.0.4
Published
React Native library for Android background location tracking
Maintainers
Readme
React Native Android Background Location
A React Native library for tracking location in the background on Android devices.
Features
- Background location tracking on Android
- Foreground service support
- TypeScript support
- Simple and easy to use API
- Customizable update interval and distance
- Works even when the app is minimized or killed (background/terminated mode)!
Installation
npm install react-native-android-bg
# or
yarn add react-native-android-bgAndroid Setup
- Add the following permissions to your
android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />- Add the following service to your
android/app/src/main/AndroidManifest.xmlinside the<application>tag:
<service
android:name="com.reactnativeandroidbg.LocationService"
android:foregroundServiceType="location"
android:enabled="true"
android:exported="false" />Usage
import BackgroundLocation from 'react-native-android-bg';
// Start location tracking with custom interval and distance (in ms and meters)
BackgroundLocation.startLocationService(2000, 20); // 2 seconds, 20 meters
// Or use default values (1000ms, 10 meters)
BackgroundLocation.startLocationService();
// Stop location tracking
BackgroundLocation.stopLocationService();
// Listen for location updates
const subscription = BackgroundLocation.addLocationListener((location) => {
console.log('Location:', location.latitude, location.longitude);
});
// Remove listener when done
subscription.remove();API
startLocationService(intervalMs?: number, minDistanceMeters?: number)
Starts the background location tracking service.
intervalMs(optional): Update interval in milliseconds. Default is1000.minDistanceMeters(optional): Minimum distance in meters for updates. Default is10.
stopLocationService()
Stops the background location tracking service.
addLocationListener(callback: (location: LocationUpdate) => void)
Adds a listener for location updates. Returns an object with a remove() method to clean up the listener.
The location object has the following structure:
interface LocationUpdate {
latitude: number;
longitude: number;
}License
MIT © sarabbajwa13
