【问题标题】:get Permission for location on Android with PermissionsAndroid使用 PermissionsAndroid 在 Android 上获取位置权限
【发布时间】:2021-11-28 08:46:33
【问题描述】:

我有点困惑。我使用的示例来自:

我正在尝试在 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 模拟器,但它没有改变任何东西。

【问题讨论】:

  • 您是否在 android/app/src/main/AndroidManifest.xml 文件中包含了权限? Like this
  • 是的,我添加了这一行:
  • 你想要获取的权限,在你的AndroidManifest.xml中,请查看。
  • 是的,我在清单中添加了以下行:

标签: android react-native android-permissions


【解决方案1】:

请尝试以下代码

再做一件事clean the gradlere-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>
    )

}

【讨论】:

  • 它有效,但为什么有效?为什么我的代码不起作用。我不明白
  • 我明白了。这是因为第一个结果总是错误的。谢谢。 :)
  • 很高兴听到这个消息 ;)
猜你喜欢
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 2015-12-13
  • 2018-11-14
  • 1970-01-01
  • 2014-02-16
相关资源
最近更新 更多