【发布时间】:2019-06-05 22:33:37
【问题描述】:
这是我第一次在 Expo 中开发,我正在构建应用程序来跟踪位置并使用 node.js 每 5 秒将数据发送到服务器。我正在使用来自世博会的TaskManager API,我会跟踪所有内容,并且它可以正常工作,我正在获取数据。但是当我将我的应用程序放在后台时,它会停止console.log(data)。
这是否应该在后台任务中运行(TaskManager),即使在使用 Expo Dev Tool 的开发环境中,还是需要在它工作之前进入生产模式?
当我像这样将应用程序切换到后台模式时,我的console.log 停止工作。
我的示例代码 App.js
const LOCATION_TRACKER = 'background-task-location';
export default class App extends Component {
state = {
mapRegion: null,
hasLocationPermissions: false,
locationResult: null,
marker: {
latitude: 0,
longitude: 0
},
latitude: 0,
longitude: 0,
location: null,
errorMessage: null
}
componentDidMount() {
//this.watchCurLocation();
this.onLoad();
}
//init task manager
onLoad = async() => {
let isRegistered = await TaskManager.isTaskRegisteredAsync(LOCATION_TRACKER)
if (!isRegistered) await Location.startLocationUpdatesAsync(LOCATION_TRACKER, {
accuracy: Location.Accuracy.High,
/* after edit */
timeInterval: 2500,
distanceInterval: 5,
})
}
onPress = async () => {
console.log('waiting')
await Location.startLocationUpdatesAsync(LOCATION_TRACKER, {
accuracy: Location.Accuracy.High,
timeInterval: 5000,
distanceInterval: 5
});
console.log('waiting for get task option');
//const data = await TaskManager.getTaskOptionsAsync(LOCATION_TRACKER)
//console.log(data);
};
watchCurLocation = () =>{
this.onPress();
setTimeout(() => {
this.watchCurLocation();
}, 5000);
}
}
TaskManager.defineTask(LOCATION_TRACKER, ({ data, error }) => {
if (error) {
console.log(error)
// Error occurred - check `error.message` for more details.
return;
}
if (data) {
const { locations } = data;
console.log(data)
// do something with the locations captured in the background
}
});
【问题讨论】:
标签: android react-native expo