react-native - Android 11 上的展览位置权限处理问题
问题描述
设置
- 使用 create-react-native-app ( https://reactnative.dev/blog/2017/03/13/introducing-create-react-native-app )创建一个基本的 expo 应用程序
- 然后 npm install expo-location (在我的情况下为 13.0.4)
应用程序.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“使用应用程序时”的权限。换句话说,它不会再次提示,而是继续进行(并在我的示例中获取位置)
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 个具有不同响应的选项
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 中的“仅这一次”选项?有没有人找到一种方法让它正常工作并在我每次打开应用程序时提示?
解决方案
推荐阅读
- python - 快速获取字典列表中的键的方法
- ios - 单击按钮时从表格中删除单元格
- amazon-web-services - Jenkins 管道输出从 aws 到 for 循环
- java - 如何在不丢失子节点的情况下替换现有节点中的信息?
- javascript - 我在可视化组合和问题的递归调用时遇到了一些麻烦?
- c# - 使用队列在两个 BackgroundWorker 之间传递数据
- validation - 如何检查codeigniter 3的mysql表中是否存在电子邮件ID,手机号码,姓名和出生日期的组合
- excel - 忽略或删除数据连接中的线路
- xamarin.ios - 如何在 Xamarin IOS Live Camera 中添加 OverlayView
- r - prcomp "colMeans(x, na.rm = TRUE) 中的错误:'x' 必须是数字"