ACCESS_FINE_LOCATION SecurityException 尽管在清单文件中指定了权限
ACCESS_FINE_LOCATION SecurityException despite specifying the permission in the manifest file
我的应用正在尝试访问设备的位置,我在 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>
我已按如下方式实现 GoogleApiClient.ConnectionCallbacks
以访问位置服务:
public class BackgroundLocationService implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = BackgroundLocationService.class.getSimpleName();
private static GoogleApiClient googleApiClient;
private static PendingIntent locationCallback;
public static int LOCATION_INTERVAL = 10000;
public static int FAST_INTERVAL = 5000;
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected to Google API");
LocationRequest request = new LocationRequest();
request.setInterval(LOCATION_INTERVAL);
request.setFastestInterval(FAST_INTERVAL);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request, locationCallback);
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, Integer.toString(i));
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, connectionResult.toString());
}
}
当我触发位置服务调用时,抛出以下异常:
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.zza(Unknown Source)
at com.google.android.gms.location.internal.zzd.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.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 来 return 准确位置。
如果我删除清单文件中的权限 "ACCESS_FINE_LOCATION"
,当我尝试显示地图时,应用程序会崩溃
这是因为默认实现使用 LocationClient
和 LocationRequest.PRIORITY_HIGH_ACCURACY
。
GoogleMap.setMyLocationEnabled
当您只请求 ACCESS_COARSE_LOCATION
权限,但切换到 LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
.
看到这个
此问题是由 Android 6.0 的一项新功能引起的。
从 Android 6.0(API 级别 23)开始,用户在应用 运行 时向应用授予权限,而不是在安装应用时。
http://developer.android.com/training/permissions/requesting.html
为了解决未明确授予权限时的问题,我进行了检查以包装位置服务调用(根据 Google 文档,稍作修改)。
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..
}
Google 文档还逐步介绍了如何请求所需的权限。
2016 年 10 月 12 日更新
添加 @Siddharth 的更新以包含对 checkSelfPermission()
更有帮助的答案
//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
}
}
对于 gyamana 在 Manifest.permission.ACCESS_FINE_LOCATION 错误的回答中有错误的其他人。确保您使用的是 android.Manifest 而不是 my.app.package.Manifest.
摘自 Google 文档。似乎这个线程上最被接受的答案缺少一些关于异步回调的关键信息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
}
}
我的应用正在尝试访问设备的位置,我在 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>
我已按如下方式实现 GoogleApiClient.ConnectionCallbacks
以访问位置服务:
public class BackgroundLocationService implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = BackgroundLocationService.class.getSimpleName();
private static GoogleApiClient googleApiClient;
private static PendingIntent locationCallback;
public static int LOCATION_INTERVAL = 10000;
public static int FAST_INTERVAL = 5000;
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected to Google API");
LocationRequest request = new LocationRequest();
request.setInterval(LOCATION_INTERVAL);
request.setFastestInterval(FAST_INTERVAL);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request, locationCallback);
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, Integer.toString(i));
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, connectionResult.toString());
}
}
当我触发位置服务调用时,抛出以下异常:
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.zza(Unknown Source)
at com.google.android.gms.location.internal.zzd.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.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 来 return 准确位置。
如果我删除清单文件中的权限 "ACCESS_FINE_LOCATION"
,当我尝试显示地图时,应用程序会崩溃
这是因为默认实现使用 LocationClient
和 LocationRequest.PRIORITY_HIGH_ACCURACY
。
GoogleMap.setMyLocationEnabled
当您只请求 ACCESS_COARSE_LOCATION
权限,但切换到 LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
.
看到这个
此问题是由 Android 6.0 的一项新功能引起的。
从 Android 6.0(API 级别 23)开始,用户在应用 运行 时向应用授予权限,而不是在安装应用时。
http://developer.android.com/training/permissions/requesting.html
为了解决未明确授予权限时的问题,我进行了检查以包装位置服务调用(根据 Google 文档,稍作修改)。
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..
}
Google 文档还逐步介绍了如何请求所需的权限。
2016 年 10 月 12 日更新
添加 @Siddharth 的更新以包含对 checkSelfPermission()
//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
}
}
对于 gyamana 在 Manifest.permission.ACCESS_FINE_LOCATION 错误的回答中有错误的其他人。确保您使用的是 android.Manifest 而不是 my.app.package.Manifest.
摘自 Google 文档。似乎这个线程上最被接受的答案缺少一些关于异步回调的关键信息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
}
}