首页 > 解决方案 > Android 11 上的展览位置权限处理问题

问题描述

设置

应用程序.js

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


const handlePress = async () => {
  // Open the custom settings if the app has one
  await Linking.openSettings();
};

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

  useEffect(() => {
    (async () => {
      const permsRequest = await Location.requestForegroundPermissionsAsync();
      console.log(JSON.stringify(permsRequest));
      let { status } = permsRequest;
      if (status !== 'granted') {
        setErrorMsg('Permission to access location was denied');
        return;
      }

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

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

  return (
    <View style={styles.container}>
      <Text>{text}</Text>
      <Button title="Open Settings" onPress={handlePress} />
    </View>
  );
}

const styles = StyleSheet.create({   container: {
  margin: 10,
  flex: 1,
  justifyContent: 'center',
}, }); 

到目前为止似乎没有人提出这个问题,所以我想知道是否只是我错过了一些东西。

在 Android 11 上,如果我的应用尚未授予任何位置权限,Android 11 将提示我选择以下 3 个选项之一:

答:使用应用程序时

B:只有这一次

C:拒绝

无论我授予什么权限,我都可以访问/检查 Location.requestForegroundPermissionsAsync();

问题在于选项 B“仅此一次”。如果我选择选项 B,我希望 Android 11 每次打开我的应用程序时都会再次提示如何使用位置,但事实并非如此。它继续表现得就像我已授予选项 A“使用应用程序时”的权限。换句话说,它不会再次提示,而是继续进行(并在我的示例中获取位置)

安卓11

Location.requestForegroundPermissionsAsync() 的响应对于选项 A 和 B 完全相同,这没有任何意义。见下文:

答:使用应用程序时

{"granted":true,"android":{"scoped":"fine","accuracy":"fine"},"canAskAgain":true,"status":"granted","expires":"never"}

B:只有这一次

{"granted":true,"android":{"scoped":"fine","accuracy":"fine"},"canAskAgain":true,"status":"granted","expires":"never"}

C:拒绝

{"granted":false,"android":{"scoped":"none","accuracy":"none"},"canAskAgain":false,"status":"denied","expires":"never"}

它在 Android 10 中正常工作,它为 Location.requestForegroundPermissionsAsync() 提供了 3 个具有不同响应的选项

安卓10

A:仅在使用应用程序时允许

{"granted":true,"android":{"scoped":"fine","accuracy":"fine"},"canAskAgain":true,"status":"granted","expires":"never"}

B:拒绝

{"granted":false,"android":{"scoped":"none","accuracy":"none"},"canAskAgain":true,"status":"denied","expires":"never"}

C:拒绝,不要再问

{"granted":false,"android":{"scoped":"none","accuracy":"none"},"canAskAgain":false,"status":"denied","expires":"never"}

只是我,还是 expo-location 似乎无法处理 Android 11 中的“仅这一次”选项?有没有人找到一种方法让它正常工作并在我每次打开应用程序时提示?

标签: react-nativeexpolocationandroid-permissionsandroid-11

解决方案


推荐阅读