Interview frequency: …
Short answer
Register a background task with TaskManager and expo-location, configure the activity type and update distance, and obtain explicit ACCESS_BACKGROUND_LOCATION permission.
Explanation
Both operating systems impose strict limits on location tracking while the screen is asleep to prevent spying and save power. Android requires a persistent foreground service notification telling the user that the app is tracking location, while iOS requires the precise reason to be declared in UIBackgroundModes.
Code example
import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';
const LOCATION_TASK_NAME = 'background-location-task';
TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
if (error) return;
if (data) {
const { locations } = data as any;
console.log("Background location:", locations);
}
});
// Start tracking:
await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
accuracy: Location.Accuracy.Balanced,
distanceInterval: 50, // Update only every 50 meters
showsBackgroundLocationIndicator: true,
});Common mistakes
- Using highest location accuracy at all times, draining the entire phone battery in under two hours.