世博会后台权限异步不起作用

Expo Background Permissions Async is not working

我正在开发一个应用程序,有时需要跟踪我的用户,因为他们在应用程序之外。我已经浏览了 expo 文档,看来我应该使用前台和后台权限异步功能,但我的 expo 应用程序无法识别任何一个版本。有人遇到过这种情况么?我该如何解决?下面是我的一些代码:

import * as  Permissions from 'expo-permissions'; 
import * as Request from 'expo-permissions';
import * as Location from 'expo-location';
import Constants from 'expo-constants'


useEffect(() => {
    (async () => {
      if (Platform.OS === 'android' && !Constants.isDevice) {
        setErrorMsg(
          'Oops, this will not work on Snack in an Android emulator. Try it on your device!'
        );
        return;
      }
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Permission to access location was denied');
        return;
      }
      let location = await Location.getCurrentPositionAsync({});
      setLocation(location);
    })();
  }, []);

  useEffect (() =>{
    (async () => {
      if (Platform.OS === 'android' && !Constants.isDevice){
        setErrorMsg2(
          'Whoops'
        );
        return;
      }
      let { status2 } = await Location.requestBackgroundPermissionsAsync();
      if (status2 !== 'granted') {
        setErrorMsg2('Permission to access background location was denied');
        return;
      }
      let location2 = await Location.getCurrentPositionAsync({})
      setLocation2(location2)
    })()
  },[])

  let text = 'Waiting..';
  if (errorMsg) {
    text = errorMsg;
  }
   else if (location) {
    text = JSON.stringify(location);
  }

  let text2 = 'Also Wating...';
    if (errorMsg2){
      text2 = errorMsg2;
    }
    else if (location) {
      text2 = JSON.stringify(location)
    }

我是运行 Expo in managed workflow,目前使用Expo 3.23.2版,但也在4.3.2版上测试过,也出现了同样的错误。

在使用 expo-location 时,您必须牢记以下几点:

  1. Location.requestBackgroundPermissionsAsync() 要求用户在应用程序处于后台时授予位置权限。在 Android 11 或更高版本上:此方法将打开系统设置页面 - 在此之前,您应该向用户解释为什么您的应用程序需要后台位置权限.

  2. 申请后台权限前应先授予前台权限(没有前台权限,您的应用无法获得后台权限).

参考文档here

注意:这已经在 SDK 41 和 expo-location 版本 12.2.1

上进行了测试

Working Example for Background Location here

我是如何实现的

import React, { useState, useEffect } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import * as Location from 'expo-location';

export default function App() {
  const [location, setLocation] = React.useState(null);
  const [errorMsg, setErrorMsg] = React.useState(null);

  React.useEffect(() => {
    (async () => {
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Permission to access location was denied');
        return;
      }

      let location = await Location.getCurrentPositionAsync({});
      setLocation(location);

      let backPerm = await Location.requestBackgroundPermissionsAsync();
      console.log(backPerm);
    })();
  }, []);

  let text = 'Waiting..';
  if (errorMsg) {
    text = errorMsg;
  } else if (location) {
    text = JSON.stringify(location);
  }

  return (
    <View style={styles.container}>
      <Text style={styles.paragraph}>{text}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: 50,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
});

首先,它询问我前台权限,我点击了 While Using the app

然后,为了获得后台位置权限,它会将我带到设备设置页面以允许在后台访问位置

如果万一这不能按预期工作,请尝试这些步骤

  1. 清除授予 Expo Go 应用程序的所有权限。
  2. 清除缓存和应用数据
  3. 卸载并重启您的设备,然后重新下载 Expo go 应用程序