Android 尽管在清单文件中指定了权限,但仍可以访问\u FINE\u LOCATION SecurityException

Android 尽管在清单文件中指定了权限,但仍可以访问\u FINE\u LOCATION SecurityException,android,permissions,android-6.0-marshmallow,Android,Permissions,Android 6.0 Marshmallow,我的应用程序正在尝试访问设备的位置,我已在AndroidManifest.xml中包含以下内容: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.app"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission

我的应用程序正在尝试访问设备的位置,我已在
AndroidManifest.xml
中包含以下内容:

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.app">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application>
        <meta-data android:name="com.google.android.gms.version" />
    </application>

</manifest>
触发位置服务调用时,会引发以下异常:

java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
    at android.os.Parcel.readException(Parcel.java:1599)
    at android.os.Parcel.readException(Parcel.java:1552)
    at com.google.android.gms.location.internal.zzi$zza$zza.zza(Unknown Source)
    at com.google.android.gms.location.internal.zzk.zza(Unknown Source)
    at com.google.android.gms.location.internal.zzl.zza(Unknown Source)
    at com.google.android.gms.location.internal.zzd$7.zza(Unknown Source)
    at com.google.android.gms.location.internal.zzd$7.zza(Unknown Source)
    at com.google.android.gms.internal.zzlb$zza.zzb(Unknown Source)
    at com.google.android.gms.internal.zzlf.zza(Unknown Source)
    at com.google.android.gms.internal.zzlf.zzb(Unknown Source)
    at com.google.android.gms.internal.zzli.zzb(Unknown Source)
    at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
    at com.localz.spotzpush.sdk.service.BackgroundLocationService.onConnected(BackgroundLocationService.java:45)
    at com.google.android.gms.common.internal.zzk.zzh(Unknown Source)
    at com.google.android.gms.internal.zzlg.zznU(Unknown Source)
    at com.google.android.gms.internal.zzlg.onConnected(Unknown Source)
    at com.google.android.gms.internal.zzli$2.onConnected(Unknown Source)
    at com.google.android.gms.common.internal.zzj$zzg.zzpf(Unknown Source)
    at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source)
    at com.google.android.gms.common.internal.zzj$zza.zzt(Unknown Source)
    at com.google.android.gms.common.internal.zzj$zzc.zzph(Unknown Source)
    at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我正在使用一个实际的设备来产生这个问题,一直在琢磨引发异常的原因。

默认执行
LocationSource
不一定要使用GPS来返回准确的位置

如果我删除清单文件中的权限
“ACCESS\u FINE\u LOCATION”
,只要我尝试显示地图,应用程序就会崩溃

这是因为默认实现将
LocationClient
LocationRequest.PRIORITY\u HIGH\u accurity
一起使用

GoogleMap.setMyLocationEnabled
当您仅请求
访问\u粗略\u位置
权限,但切换到
LocationRequest.PRIORITY\u BALANCED\u POWER\u Accurance


请参见此

问题是由Android 6.0的新功能引起的

从Android 6.0(API级别23)开始,用户在应用程序运行时授予应用程序权限,而不是在安装应用程序时

为了在没有明确授予权限的情况下解决这个问题,我检查了位置服务调用(根据Google文档,稍作修改)

Google文档还介绍了如何请求所需的权限

于2016年10月12日更新

添加@的更新以包括对
checkSelfPermission()


对于其他所有在Manifest.permission.ACCESS\u FINE\u位置错误处对gyamana的回答有错误的人。确保您使用的是android.Manifest,而不是my.app.package.Manifest。

从谷歌文档中摘录。似乎这个线程上最被接受的答案是缺少一些关于异步
checkSelfPermission
回调的关键信息

请确认。如果没有,我将删除此答案

// 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.READ_CONTACTS)) {

        // 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.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

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

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } 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
    }
}

您的
minSdkVersion
targetSdkVersion
是什么?嘿@JBirdVegas,它们分别是15和23。我正在获取java.lang.SecurityException:客户端必须具有访问精细位置权限才能请求优先级高精度位置。与Android 5.1上的Target SDK 22崩溃(设备为S plus(GiONEE_WBL7511))有任何线索吗?请查看我的答案并进行适当的更正。如果您能对答案进行更正,我将删除我的答案。@Siddharth,好的,谢谢您,现在将更新我的答案。另外,将Android Studio更新为最新版本将允许它自动检查、标记和解决此问题。@arpitgoyal2008您解决了此问题吗?我在Galaxy J3和J3 pro上面临着类似的问题。对于运行在安卓5.1上的手机来说很奇怪,怎么能向服务请求许可?
int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);

if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
    //Execute location service call if user has explicitly granted ACCESS_FINE_LOCATION..
}
//For this example, run the check onResume()
@Override
public void onResume() {

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, 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.
            // PERMISSION_REQUEST_ACCESS_FINE_LOCATION can be any unique int
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_ACCESS_FINE_LOCATION);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_ACCESS_FINE_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } 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
    }
}
// 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.READ_CONTACTS)) {

        // 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.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

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

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } 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
    }
}