如何在运行时请求对React Native中Android设备位置的许可? [英] How do I request permission for Android Device Location in React Native at run-time?

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

问题描述

我一直在尝试将React Native的GeoLocalisation用于Android应用程序.此处记录不完整的模块 https://facebook.github.io/react -native/docs/geolocation.html . 根据文档,您可以在AndroidManifest.xml文件

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设备位置的许可?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆