我有点糊涂了。我使用的例子来自:
& lt; reactnative.dev/docs/permissionsandroid>
和我试图获得许可的位置在android上,但它总是说:
permission is null
下面是我的代码:
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {useEffect} from 'react';
import {
StatusBar,
PermissionsAndroid,
Platform
} from 'react-native';
import Geolocation from '@react-native-community/geolocation';
//import SearchResults from './src/screens/SearchResults';
import DestinationSearch from './src/screens/DestinationSearch';
//import HomeScreen from './src/screens/HomeScreen';
navigator.geolocation = require('@react-native-community/geolocation');
const App: () => React$Node = () => {
const androidPermission = async() => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINA_LOCATION,
rationale ={
title: "App location Permission",
message:
"App needs access to your location " +
"so you can take awesome rides.",
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK"
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the location");
} else {
console.log("location permission denied");
}
} catch (err) {
console.warn(err);
}
}
useEffect(create= () => {
if(Platform.OS ==='android'){
//request android permission
androidPermission();
}else{
//request IOS permission
Geolocation.requestAuthorization();
}
}, inputs=[])
return (
<>
<StatusBar barStyle= 'dark-content' />
<DestinationSearch />
</>
);
};
export default App;
有人能帮我吗?我甚至尝试在设置选项中给应用程序权限,但它仍然显示"权限为空"。我重新启动我的android模拟器,但它没有改变任何东西。
请尝试下面的代码
再做一件事clean the gradle
和re-install the app
和
import React, { useEffect, useState, useRef } from 'react'
import { SafeAreaView, Text, PermissionsAndroid } from 'react-native'
export default function Welcome() {
useEffect(() => {
setTimeout(() => {
_checkPermission()
}, 1000)
}, [])
const _checkPermission = async () => {
try {
const result = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
console.log('result', typeof result)
console.log('result', result)
if (result == true) {
// you code
}
else if (result == false) {
const status = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
if (status === 'never_ask_again') {
// Your code
}
else if (status === 'denied') {
_checkPermission()
}
else if (status === 'granted') {
// Your code
}
}
} catch (error) {
console.log('error', error)
}
}
return (
<SafeAreaView>
<Text>
Welcome.js
</Text>
</SafeAreaView>
)
}