我正在将React Native与Expo Location.startLocationUpdatesAsync一起使用。我在同一个文件中创建了三个函数,前两个在我的导出默认App函数中,其中TaskManager在文件中全局定义。
async startBackgroundUpdate() {
await Location.startLocationUpdatesAsync(taskName, {
accuracy: Location.Accuracy.Highest,
timeInterval: 0,
distanceInterval: 0,
forgroundService: {
notificationTitle: "I'm a notification title",
notificationBody: "I'm a notification body",
notificationBody: "#abcdef"
}
});
}
async stopBackgroundUpdate() {
await Location.stopLocationUpdatesAsync(taskName)
}
TaskManager.defineTask(taskName, async ({ data, error }) => {
if (error) {
return console.log(error)
}
if (data) {
const { locations } = data;
locationGlobal = locations[0];
let date = new Date(locationGlobal.timestamp).toISOString().slice(-13, -5)
// App.setLocation(location)
console.log("Task has been running "+ count +" time(s). With data: "+data);
console.log(locationGlobal);
console.log(count++);
console.log(date)
}
});
我还有一个全局变量,用于保存taskName
const taskName = 'geofencing'
停止和启动背景定位功能连接到一个按钮。我可以很好地启动后台位置,但是当我尝试停止它时,我得到了错误
Task 'geofencing' not found for app ID '@anonymous/track-it-fecc322d-0ade-4239-8bd1-fdcb47b0397e'.
- node_modules\react-native\Libraries\BatchedBridge\NativeModules.js:104:55 in <unknown>
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:414:4 in __invokeCallback
- ... 4 more stack frames from framework internals
对此我是陌生的,我想我错过了一个难题。我可以在App函数内部看到taskName并创建任务,但是在同一级别上,我无法引用它来停止它,有什么想法吗?