Background geolocation in Android phones

Please provide the following:

  1. SDK Version: 37.0.0
  2. Platforms(Android/iOS/web/all): Android / iOS

I’m in need of your advices and help. I develop an application (intended to use in both Android and iOS), using Expo CLI with React Native. I am using expo-location to get the device’s location, and even though I have set the interval to a certain time in startLocationUpdatesAsync, it doesn’t fire in the given interval, but mostly in double or triple the time needed when the phone is locked. However, if I do use my phone, the intervals are better and more consistent. Is there any way to fix it? I have attached below the code block for reference:

const options_updates = {
    accuracy: Location.Accuracy.BestForNavigation, //accuracy must be best for navigation,
    timeInterval: 300000, //interval for android
    foregroundService: { //foreground serrvice to keep it running in background on android
      notificationTitle: "Always with you", //notification for the background location - title
      notificationBody: "I am following you" //notification for the background location - body
    },
    showsBackgroundLocationIndicator: true, //changes status bar appearance on ios
    pausesUpdatesAutomatically: false, //will not pause location updates on ios
  }

TaskManager.defineTask(TASKNAME_BACKGROUND, ({ data: { locations }, error }) => { //defines the background location task
    console.log("task run!"); //logs that the task runs
    if (error) { //if we get an error
      console.log("Got error while sending", error); //logs the error
      Location.stopLocationUpdatesAsync(TASKNAME_BACKGROUND);//cleans up the task
      return;
    }

    console.log('Received new locations on ' + locations[0].coords.latitude + ', ' + locations[0].coords.longitude, "timestamp: " + timestamp_get()); //logs the coordinates

  });

Location.startLocationUpdatesAsync(TASKNAME_BACKGROUND, options_updates); //starts background task to retrieve location

For those of you who will probably ask why I didn’t use deferredUpdatesInterval, it is because it stops working after some time, probably because of the same reason. The second problem with it, is that it forgets I set this option once I unlock my phone.

Did you find a solution to your problem? I have a similar problem with reliability of the options when using foregroundService on android.

I could imagine the problem in your case could be timeInterval only defines a minimum time to wait” and not the exact intervall of the execution. The docs imply that there is no real guaratee, even if I thougt the event would be called pretty close to that intervall when using BestForNavigation.

Possible solution: Decrease the time intervall to opt in for more frequent updates and manually check if you want to “use” the location in your task or just skip it (depends a bit on your usecase…)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.