棉花糖精细和粗略位置许可

Marshmallow FINE and COARSE location permission

我正在尝试访问 ACCESS_FINE_LOCATION,如果找不到,请访问 ACCESS_COARSE_LOCATION。所以我请求这两个许可,但他们给了我相同的对话框,要求获得位置许可。我知道他们都在同一组,但 Google 说:

Note: Your app still needs to explicitly request every permission it needs, even if the user has already granted another permission in the same group. In addition, the grouping of permissions into groups may change in future Android releases. Your code should not rely on the assumption that particular permissions are or are not in the same group.

这意味着我在一秒钟内请求了这 2 个权限,这导致连续 2 个对话框。这对我来说似乎不太友好。有没有更好的方法?

定义ACCESS_FINE_LOCATION权限时不需要ACCESS_COARSE_LOCATION权限。

来自 Android 文档:

Requesting User Permissions

In order to receive location updates from NETWORK_PROVIDER or GPS_PROVIDER, you must request user permission by declaring either the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission, respectively, in your Android manifest file. For example:

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

Without these permissions, your application will fail at runtime when requesting location updates.

Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. (Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.)

请看https://developer.android.com/guide/topics/location/strategies.html

我已经解决了同样的problem.Try给它运行时权限,首先检查清单SDK版本,如果你的版本大于6.0或等于6.0那么你需要检查权限以获得你的权限并通过permission Fine location only in request location 那么肯定可以。

如果您在代码中遇到困难,那么您可以索取代码,我会更新您,但首先要完成这些步骤。 如果您成功完成任务,请给它评分。