“gps”位置提供程序需要Android 6.0的ACCESS_FINE_LOCATION权限

时间:2015-11-23 07:11:07

标签: android

在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中测试过,工作正常。

请帮助!!。提前致谢。

4 个答案:

答案 0 :(得分:55)

自6.0以来,一些权限被视为“危险”(FINE_LOCATION就是其中之一)。

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

要做到这一点:

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

它将显示一个DialogBox,用户可以在其中选择是否自动调整您的应用以使用位置。

然后使用此功能让用户回答:

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文档中进行了解释:http://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:5)

试试此代码

private void marshmallowGPSPremissionCheck() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && getActivity().checkSelfPermission(
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && getActivity().checkSelfPermission(
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSION_LOCATION);
        } else {
            //   gps functions.
        }
    }

也加上..................

@Override
    public void onRequestPermissionsResult(int requestCode,
                                           String[] permissions, int[] grantResults) {
        if (requestCode == MY_PERMISSION_LOCATION
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //  gps functionality
        }
    }

答案 2 :(得分:0)

自版本6以来,我们对Android权限进行了一些更改。您必须在使用它们时同意权限,而不是在安装应用程序时。如果您使用buildtools版本23,可能会出现一些问题。检查this link以了解相关信息。转到设备设置&gt;应用并选择您的应用,然后手动接受权限并检查问题是否已解决。

答案 3 :(得分: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