我正在运行一个EAS应用程序,我必须使用Expo-Task-Manager来处理后台位置。当我的应用程序构建时,我遇到了这个错误:
TaskManager: Task "firstTask" has been executed but looks like it is not defined. Please make
sure that "TaskManager.defineTask" is called during initialization phase.
下面是我在应用程序中执行任务管理器的代码,但我很难找到在“初始化阶段”中调用它的位置。
import * as TaskManager from 'expo-task-manager'
import * as BackgroundFetch from 'expo-background-fetch'
import * as Location from 'expo-location'
const LOCATION_TASK_NAME = 'background-location-task'
useFocusEffect(
React.useCallback(()=>{
const requestBackgroundPermissions = async() =>{
const {status} = await Location.requestBackgroundPermissionsAsync()
if(status === 'granted'){
await Location.startLocationUpdatesAsync('firstTask',{
accuracy: Location.Accuracy.Balanced,
});
}
requestBackgroundPermissions()
},
[],
),
)
//在useFocusEffect之外
TaskManager.defineTask('firstTask',({data,errror})=>{
if(error){
alert('Something went wrong with background locations')
}
if(data){
alert('Something went right with background locations')
const{locations} = data
}
})
2条答案
按热度按时间7d7tgy0s1#
因此,如果您遇到此问题,请尝试将任务管理器移动到App.js文件中。当应用程序加载时,任务管理器将是初始化阶段的一部分。如果您有任何问题,请随时联系,但它应该看起来像这样:
xyhw6mcr2#
我已经在我的
index.js
文件中使用了这个解决方案,它工作得很好