如何在运行时在 React Native 中请求 Android 设备位置

How do I request permission for Android Device Location in React Native at run-time?(如何在运行时在 React Native 中请求 Android 设备位置的权限?)

本文介绍了如何在运行时在 React Native 中请求 Android 设备位置的权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将 React Native 的 GeoLocalisation 用于 Android 应用程序.文档记录不佳的模块可在此处找到 https://facebook.github.io/react-本机/docs/geolocation.html.根据文档,您使用 AndroidManifest.xml 文件中的以下代码处理 Android 上的位置权限

I have been trying to use React Native 's GeoLocalisation for an Android App. The poorly documentated module is found here https://facebook.github.io/react-native/docs/geolocation.html. According to the documentation, you handle location permissions on Android using the following code in the AndroidManifest.xml file

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

但是,我的在线研究表明,上述代码行对于 ANDROID >= 6.0

However, my online research suggests that the above line of code is useless for versions of ANDROID >= 6.0

由于我的 GeoLocation 实现当前无法正常工作,我没有其他理由相信位置权限没有得到正确处理.

As my implementation of GeoLocation is not currently working, I have no other reason but to believe that location permissions are not correctly handled.

如何在运行时为 React Native Android App 成功请求位置权限?提前致谢!

How do I successfully request location permission at run-time for React Native Android App ? Thanks in advance !

推荐答案

这对我不起作用

if (granted === PermissionsAndroid.RESULTS.GRANTED) {
}

我提到了 https://facebook.github.io/react-native/docs/permissionsandroid.html#request并且 check() 返回一个布尔值

I referred to https://facebook.github.io/react-native/docs/permissionsandroid.html#request and check() return a boolean

const granted = await PermissionsAndroid.check( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION );

if (granted) {
  console.log( "You can use the ACCESS_FINE_LOCATION" )
} 
else {
  console.log( "ACCESS_FINE_LOCATION permission denied" )
}

这篇关于如何在运行时在 React Native 中请求 Android 设备位置的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在运行时在 React Native 中请求 Android 设备位置

基础教程推荐