首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >世博后台权限异步不起作用

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

Stack Overflow用户
提问于 2021-07-01 01:06:53
回答 1查看 4.3K关注 0票数 1

我正在开发一个应用程序,我的用户有时需要跟踪,因为他们是在应用程序之外。我已经看过世博文档,看起来我应该使用前台和后台权限,异步函数,但是我的世博应用程序不承认这两个版本。还有其他人遇到过这个吗?我该怎么解决这个问题?下面是我的一些代码:

代码语言:javascript
复制
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)
    }

我正在管理的工作流中运行博览,目前使用的是世博3.23.2版本,但我也在4.3.2版上测试过它,并且抛出了相同的错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-01 05:55:59

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

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

请参阅docs 这里

注意:这已经在SDK 41和expo-location**'s版本**上进行了测试。

此处背景位置的工作示例

我是如何实现它的

代码语言:javascript
复制
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应用程序
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68203138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档