[英]Background location tracking not working - Expo - Android
I am working on something where I need to track background location if the app is in background and also if the device is asleep.如果应用程序处于后台并且设备处于睡眠状态,我正在研究需要跟踪后台位置的内容。 I currently have it working for app in background but it stops tracking when the device is asleep.
我目前让它在后台为应用程序工作,但是当设备处于睡眠状态时它会停止跟踪。 I am using Expo for the app and using Expo Task Manager alongside Expo Location to fetch location in background.
我正在为应用程序使用 Expo,并使用 Expo Task Manager 和 Expo Location 在后台获取位置。 Anyone have any idea how to fetch location while app is in background and device is in sleep mode ?
任何人都知道如何在应用程序处于后台且设备处于睡眠模式时获取位置?
Here's the code这是代码
import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import * as Location from 'expo-location';
import * as TaskManager from 'expo-task-manager';
const App = () => {
useEffect(() => {
(async () => await _askForLocationPermission())();
});
this.backgroundLocationFetch = async () => {
const { status } = await Location.requestBackgroundPermissionsAsync();
if (status === 'granted') {
console.log('cmon dance with me!')
await Location.startLocationUpdatesAsync('FetchLocationInBackground', {
accuracy: Location.Accuracy.Balanced,
timeInterval: 3000,
distanceInterval: 1,
foregroundService: {
notificationTitle: 'Live Tracker',
notificationBody: 'Live Tracker is on.'
}
});
}
}
const _askForLocationPermission = async () => {
(async () => {
let { status } = await Location.requestBackgroundPermissionsAsync();
if (status !== 'granted') {
setgpsErrorMsg('Permission to access location was denied');
}
})();
};
return(
<View>
<Text></Text>
</View>
)
};
TaskManager.defineTask('FetchLocationInBackground', ({ data, error }) => {
if (error) {
console.log("Error bg", error)
return;
}
if (data) {
const { locations } = data;
console.log("BGGGG->", locations[0].coords.latitude, locations[0].coords.longitude);
}
});
export default App;
I had precisely the same problem and solved this by using and EventEmitter to dispatch the location updates to the UI component.我遇到了完全相同的问题,并通过使用 EventEmitter 将位置更新分派到 UI 组件来解决此问题。
Top of file:文件顶部:
import EventEmitter from 'EventEmitter'
const locationEmitter = new EventEmitter();
didMount:已安装:
locationEmitter.on(LOCATION_UPDATE, (locationData) => {
console.log('locationEmitter locationUpdate fired! locationData: ', locationData);
let coordinatesAmount = locationData.newRouteCoordinates.length - 1;
this.setState({
latitude: locationData.newRouteCoordinates[coordinatesAmount - 1].latitude,
longitude: locationData.newRouteCoordinates[coordinatesAmount - 1].longitude,
routeCoordinates: this.state.routeCoordinates.concat(locationData.newRouteCoordinates)
})
})
componentWillUnmount:组件将卸载:
locationEmitter.off(LOCATION_UPDATE);
inside background task definition:内部后台任务定义:
locationEmitter.emit(LOCATION_UPDATE, locationData)
This succesfully sends the location data from the background task, but I'm still stuck in the problem of how to make the background task send location update batches more often.这成功地从后台任务发送了位置数据,但我仍然陷入了如何让后台任务更频繁地发送位置更新批次的问题。 My related post is here .
我的相关帖子在这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.