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

2023-03-22移动开发问题
3

本文介绍了如何在运行时在 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 设备位置的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何在 COCOS2d Android 中使用 CClistview?
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)...
2024-08-12 移动开发问题
5

cocos2d-android:如何显示分数
cocos2d-android: how to display score(cocos2d-android:如何显示分数)...
2024-08-11 移动开发问题
7

Sqlite 数据库未从资产文件夹 Android 复制
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)...
2024-04-15 移动开发问题
8

SQLite 数据库副本在由设备而不是模拟器生成时出现损坏
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)...
2024-04-15 移动开发问题
4

安卓文件拷贝
Android file copy(安卓文件拷贝)...
2024-04-15 移动开发问题
6

Android如何在android中检测Edittext的Copy事件
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)...
2024-04-15 移动开发问题
5