Background Task Shuts Down

Some very curious issue going on. I’ve been having trouble getting background location to continue to function when the app is closed.

The background location task in my app gets a location update (via timeInterval)

In the app file itself (outside of component)

if (TaskManager.isTaskDefined(LOCATION_TASK_NAME))
  TaskManager.unregisterTaskAsync(LOCATION_TASK_NAME)
TaskManager.defineTask(LOCATION_TASK_NAME, handleBGLocationUpdate)

export async function handleBGLocationUpdate({ data, error }) {
  console.warn("handleBGlocationUpdate()")
  // more code that sends an HTTP request
}

In my component:

_startWatchingLocation = async () =>{
    console.warn("start watching location")
    TaskManager.unregisterTaskAsync(LOCATION_TASK_NAME)
    let isRegistered = await TaskManager.isTaskRegisteredAsync(LOCATION_TASK_NAME)
    console.warn("is registered: "+isRegistered)
    if (!isRegistered) await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
        accuracy: Location.Accuracy.High,
        timeInterval: 5000,
        distanceInterval: 0,
        /* foregroundService: {
          notificationTitle: "Cleanspoon Monitor",
          notificationBody: "Listens for new restaurant updates",
          notificationColor:"#DDFFDD" 
        },
        showsBackgroundLocationIndicator: true */
    })

So I’ve tried multiple ways, including exporting as standalone app. In standalone app here’s what happens.

While app is open, everything works great.

When app is minimizes, things work great as long as foreground service is running

When app is closed (via recent tabs, then swiping the app away), the foreground service icon stays for a few seconds, then disappears - as if the app itself terminated.

I managed to log the chunk of lines when the foreground service icon went away via logcat.

07-29 19:15:04.521  7941  7941 I TaskService: Handling intent with task name 'background-location-updates' and appId '<APPNAME ON EXPO>'.
07-29 19:15:04.530  7941  7941 I TaskService: Handling job with task name 'background-location-updates' for app with ID '<APPNAME ON EXPO>'.
07-29 19:15:04.531  7941  7941 E Expo    : Cannot initialize app loader. <init> [class android.content.Context]
07-29 19:15:04.531  7941  7941 E TaskService: Cannot execute background task because application loader can't be found.
07-29 19:15:04.531  7941  7941 I TaskService: Unregistering task 'background-location-updates' for app '<APPNAME ON EXPO>'.
07-29 19:15:04.548  1150  1355 I GnssLocationProvider: WakeLock acquired by sendMessage(SET_REQUEST, 0, com.android.server.location.GnssLocationProvider$GpsRequest@f447afb)
07-29 19:15:04.549   802  1309 I sensors-hal: batch_physical_sensor:192, android.sensor.accelerometer/16, period=10000000, max_latency=0
07-29 19:15:04.549   802  1309 I sensors-hal: batch_physical_sensor:201, android.sensor.accelerometer/16, period=10000000, max_latency=0 request completed
07-29 19:15:04.551  1150  1172 I GnssLocationProvider: WakeLock released by handleMessage(SET_REQUEST, 0, com.android.server.location.GnssLocationProvider$GpsRequest@f447afb)
07-29 19:15:04.556   802  1309 I sensors-hal: activate_physical_sensor:129, android.sensor.accelerometer/16 en=0
07-29 19:15:04.557   802  1309 I sensors-hal: activate_physical_sensor:140, android.sensor.accelerometer/16 en=0 completed
07-29 19:15:04.557   802  1309 I sensors-hal: activate_physical_sensor:129, android.sensor.magnetic_field_uncalibrated/4 en=0
07-29 19:15:04.559   802  1309 I sensors-hal: activate_physical_sensor:140, android.sensor.magnetic_field_uncalibrated/4 en=0 completed
07-29 19:15:04.559   802  1309 I sensors-hal: activate_physical_sensor:129, android.sensor.gyroscope_uncalibrated/9 en=0

Even odder - opening up Google Maps triggers the task to run! So it’s like if any app gets GPS it ends up working…

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