Ionic android 请求位置许可

Ionic android ask for location permission

我想完成的事情:
如果用户的位置服务阻止我默认获取 his/her phone 的位置,我想在 16-25 之间的每个 API 请求位置许可(Android 4.1.2-7.1.1)
从 iOS 开始,我还没有遇到这个问题,我想关注 android。

我已经尝试过的:

上面提到的插件总是 returns 和 hasPermission: true 即使访问位置在设备上被拒绝所以它不适合我。

我还没有尝试过的:

诊断插件描述说我在 API 23 及以上或 API 22 及以下之间进行选择。 (<preference name="android-targetSdkVersion" value="22"/> 是最低目标版本。)但我不知道它是否仍然适用于较低的 API 版本。

我的设置:

Diagnostic plugin description says that I have choose between API 23 and above or API 22 and below in my understanding. ( is the minimum target version.) Yet I don't know if it would still work on lower API versions.

cordova-diagnostic-plugin API 级别要求基于用于构建的 SDK 版本,而不是目标设备上的 API 版本 运行ning。 因此,使用针对 API 23 (Android 6.0) 的 SDK 构建的应用程序仍可在 运行 旧版本 Android.

设备上运行

您需要将构建目标设置为 API 23(或更高版本)以使用插件的主要版本(cordova.plugins.diagnostic 而不是 cordova.plugins.diagnostic.api-22)进行构建,因为它包括运行-time 权限代码,需要 API 23+.

的构建环境

If the user's location service is preventing me from getting his/her phone's location by default I'd like to ask for location permission on every API between 16-25 (Android 4.1.2-7.1.1)

这有两个不同的方面:

ask for location permission on every API between 16-25 (Android 4.1.2-7.1.1)

Android 运行-时间权限在 API 23 (Android 6.0)

中引入

您可以使用isLocationAuthorized()检查应用程序是否有位置授权。 在设备 运行ning Android 5 (API 22) 或更小的设备上调用它总是 return TRUE,因为权限已在安装时授予。

您可以请求 运行时间许可以使用 requestLocationAuthorization() 的位置。 在 运行ning Android 5 (API 22) 或以下设备上调用它不会有任何效果,因为权限已在安装时授予。

location service is preventing me from getting his/her phone's location

您可以使用 isLocationEnabled().

检查用户是否已关闭定位

如果位置关闭,您可以使用 switchToLocationSettings() to open the device location settings page to allow user to enable location services, or you can use the cordova-plugin-request-location-accuracy 请求所需的位置精度,而无需用户在位置设置页面上手动执行此操作。

免责声明:我是 cordova-diagnostic-plugin and cordova-plugin-request-location-accuracy.

的作者