“gps”位置提供者需要 ACCESS_FINE_LOCATION 权限以用于 android 6.0

新手上路,请多包涵

在 android 清单文件中,我为 gps 添加了以下权限以访问该位置

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

我正在使用位置管理器类进行如下的位置更新

 private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
 private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
 locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    MIN_TIME_BW_UPDATES,
                    MIN_DISTANCE_CHANGE_FOR_UPDATES,
                    this
                );

但是上面的行给出了一个例外说明

"gps" location provider requires ACCESS_FINE_LOCATION permission

这件事只发生在targetSDKVersion 23(即android 6.0)上,我也在android 5.1中测试过,效果很好。

请帮忙!!。在此先感谢。

原文由 bhanu.cs 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.8k
2 个回答

从 6.0 开始,一些权限被认为是“危险的”(FINE_LOCATION 就是其中之一)。

为了保护用户,他们必须在运行时获得授权,以便用户知道这是否与他的行为有关。

去做这个 :

  ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

它将显示一个对话框,用户将在其中选择是否自动让您的应用程序使用位置。

然后使用此函数获取用户答案:

>  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
>     switch (requestCode) {
>         case 1: {
>             // If request is cancelled, the result arrays are empty.
>             if (grantResults.length > 0
>             && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
>
>             } else {
>                 // permission denied, boo! Disable the
>                 // functionality that depends on this permission.
>             }
>         return;
>         }
>             // other 'case' lines to check for other
>             // permissions this app might request
>     }
> }
>
> ```

如果用户接受一次,那么您的应用程序将记住它,您将不再需要发送此 DialogBox。请注意,如果他决定,用户可以稍后禁用它。然后在请求位置之前,您必须测试是否仍然授予权限:

public boolean checkLocationPermission() { String permission = “android.permission.ACCESS_FINE_LOCATION”; int res = this.checkCallingOrSelfPermission(permission); return (res == PackageManager.PERMISSION_GRANTED); }

”`

这一切都在Android文档中进行了解释( onRequestPermissionsResult 也来自那里):http: //developer.android.com/training/permissions/requesting.html

原文由 Sebastien FERRAND 发布,翻译遵循 CC BY-SA 4.0 许可协议

我最近也遇到过这个问题。在 Android 6.0 中,您需要在应用程序中使用它们时明确请求所需的权限。如果您不这样做,则位置权限将被自动拒绝。

     // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(thisActivity,
                    Manifest.permission.READ_CONTACTS)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_FINE_LOCATION);

            // MY_PERMISSIONS_REQUEST_FINE_LOCATION is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }

更多信息在这里:http: //developer.android.com/training/permissions/requesting.html

原文由 Kelly 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题