使用 PermissionsAndroid 获取 Android 上的位置权限

get Permission for location on Android with 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 and re-install the app and

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>
    )

}