Home > other >  Continually Running Background Service on latest version android 11 and 12
Continually Running Background Service on latest version android 11 and 12

Time:06-21

I googled a lot and search also on a over stack flow i did not get any proper solution for Continously location update when mobile on background mode.

I have also use workmanager Periodic request but its Minimum interval is 15 minute then how get the location get sceconds.

I am working on react native tracking project i have create a native module for run services. but my service killed after sometime in background mode. but in working on forground service .

If any one use background location updates on mobile version 11 and 12 .Let me know and also share github link or code.

Thank you

CodePudding user response:

You can only take continuous background location using Foreground service. Background service are not meant for this. Refer this link on how to create and use a foreground service: https://developer.android.com/guide/components/foreground-services

CodePudding user response:

Check this React-native library no need to create custom native modules , check : react-native-geolocation-service

Why should you use this library

This library is created in an attempt to fix the location timeout issue on android with the react-native's current implementation of Geolocation API. This library tries to solve the issue by using Google Play Service's new FusedLocationProviderClient API, which Google strongly recommends over android's default framework location API. It automatically decides which provider to use based on your request configuration and also prompts you to change the location mode if it doesn't satisfy your current request configuration.

Usage:

...
import Geolocation from 'react-native-geolocation-service';
...

componentDidMount() {
  if (hasLocationPermission) {
    Geolocation.getCurrentPosition(
        (position) => {
          console.log(position);
        },
        (error) => {
          // See error code charts below.
          console.log(error.code, error.message);
        },
        { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
    );
  }
}

Edit:

you could use watchPosition

Geolocation.getCurrentPosition(
        (position) => {
          console.log(position);
        },
        (error) => {
          // See error code charts below.
          console.log(error.code, error.message);
        },
        { enableHighAccuracy: true, interval: 1000 }
    );

remember to stop all these observers.

  • Related