npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-native-android-location-service-v2

v3.0.1

Published

Android-only foreground and backgroundlocation tracking service for React Native

Readme

🚀 react-native-android-location-service-v2

Reliable Android-only foreground + background + killed-state location tracking for React Native.

Perfect for:

  • Delivery / Fleet tracking
  • Fitness & route logging
  • Passive background movement detection
  • High-accuracy GPS + geofence-based tracking
  • Running JS even when the app is killed

This library provides:

  • 🔹 Continuous GPS tracking
  • 🔹 Geofence-driven tracking (low battery use)
  • 🔹 JS foreground listeners
  • 🔹 JS background headless task
  • 🔹 Simple & stable RN API
  • 🔹 Native Kotlin implementation

📦 Installation

yarn add react-native-android-location-service-v2
# or
npm install react-native-android-location-service-v2

Autolinking works for RN 0.60+


⚙️ Android Setup

Add required permissions to your app's 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.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

❗ No service or receiver declarations needed

The library auto-registers:

  • LocationServiceV2
  • LocationGeofenceServiceV2
  • GeofenceReceiverV2

Do not add them manually.


📡 Usage

▶️ Start continuous GPS tracking

import LocationService from "react-native-android-location-service-v2";

LocationService.startLocationService(3000); // every 3 seconds

▶️ Start Geofence-based tracking (battery efficient)

LocationService.startLocationServiceWithGeofence();

🛑 Stop tracking

LocationService.stopLocationService();

❓ Check if tracking is active

const active = await LocationService.isLocationTrackingActive();
console.log("Tracking active?", active);

🎧 Foreground JS Listener (runs when app is open)

const unsubscribe = LocationService.onLocationUpdate(({ latitude, longitude, accuracy }) => {
  console.log("Foreground location:", latitude, longitude, accuracy);
});

// later
unsubscribe();

🪝 React Hook Usage

useEffect(() => {
  return LocationService.onLocationUpdate(loc => {
    console.log("Hook location:", loc);
  });
}, []);

🛰 Background / Killed-State JS Handler (Headless Task)

Runs when:

  • App is backgrounded
  • App is killed
  • Device is locked

1️⃣ Register background handler once in JS (App.js or index.js)

import LocationService from "react-native-android-location-service-v2";

LocationService.registerBackgroundHandler(async ({ latitude, longitude, accuracy }) => {
  console.log("📡 Background:", latitude, longitude, accuracy);

  await fetch("https://your-server.com/locations", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      lat: latitude,
      lng: longitude,
      acc: accuracy,
      timestamp: Date.now(),
    }),
  });
});

2️⃣ Register Headless Task in index.js

import { AppRegistry } from "react-native";
import App from "./App";
import { name as appName } from "./app.json";
import LocationService from "react-native-android-location-service-v2";

// MUST match the native name "LocationBackgroundTask"
AppRegistry.registerHeadlessTask(
  "LocationBackgroundTask",
  () => LocationService.__backgroundHandler
);

AppRegistry.registerComponent(appName, () => App);

📤 Location event example

{
  "latitude": 27.7172,
  "longitude": 85.3240,
  "accuracy": 4.2
}

⚡ Geofence Tracking Mode

The library includes a Kotlin-based geofence engine:

  • Creates a geofence around the user
  • Fires when the user exits
  • Fetches fresh GPS
  • Sends update to foreground JS
  • Sends update to background JS (headless task)
  • Recreates new geofence
  • Runs forever

Start it:

LocationService.startLocationServiceWithGeofence();

This is much more battery-friendly than continuous GPS.


📘 TypeScript Definitions

declare module "react-native-android-location-service-v2" {
  export interface LocationData {
    latitude: number;
    longitude: number;
    accuracy: number;
  }

  export function startLocationService(interval: number): void;
  export function startLocationServiceWithGeofence(): void;
  export function stopLocationService(): void;
  export function isLocationTrackingActive(): Promise<boolean>;

  export function onLocationUpdate(
    cb: (data: LocationData) => void
  ): () => void;

  export function registerBackgroundHandler(
    cb: (data: LocationData) => void
  ): void;

  export function useLocationUpdates(
    cb: (data: LocationData) => void
  ): void;

  const _default: any;

  export default _default;
}

🧩 API Summary

| Method | Description | |--------|-------------| | startLocationService(interval) | Start GPS tracking | | startLocationServiceWithGeofence() | Start geofence-driven tracking | | stopLocationService() | Stop all tracking | | isLocationTrackingActive() | Returns true/false | | onLocationUpdate(cb) | Foreground JS listener | | registerBackgroundHandler(cb) | Background JS listener (killed state) | | useLocationUpdates(cb) | React hook wrapper |


⚠️ Important Notes

  • Headless JS only runs on real devices, not emulator
  • Background tracking requires "ACCESS_BACKGROUND_LOCATION"
  • Android 14 requires foregroundServiceType="location" (already configured)
  • Foreground notification is required by Android OS
  • JS callbacks stop when app is killed → background handler continues

👨‍💻 Author

Saurav Ghimire


📄 License

MIT