- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的应用正在尝试访问设备的位置,并且我在 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$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)
我正在使用一个真实的设备来产生这个问题,并且一直在摸索为什么抛出异常。
最佳答案
此问题是由 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
}
}
关于android - 尽管在 list 文件中指定了权限,但 ACCESS_FINE_LOCATION SecurityException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33406762/
我正在使用权限 ACCESS_FINE_LOCATION 但是当运行时出现错误:需要 ACCESS_FINE_LOCATION 权限为什么? 提前致谢。 最佳答案 ACCESS_FINE_LOCAT
我是 android 开发的新手,现在我很难获取当前用户位置。 list : 主要 Activity : int permissionCheck = ContextCompat.checkSel
下面是我的AndroidManifest.xml... 如您所见,我添加了获取位置信息所需的适当权限。但
我有这个代码 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != Pack
我有点害羞。但是找不到问题所在。 这是我的 list 。这是我的代码: pub
我只在使用模拟器时遇到了一个奇怪的错误。我发现了一个 9 个月前有相同问题的问题,但根本没有答案... 我正在使用谷歌播放服务位置来获取我的应用程序中的位置,并且我确定我的 list 权限并且一切都在
我在我的应用程序中请求 FINE_LOCATION 时遇到了一些问题。我试图阅读一些关于 ACCESS_FINE_LOCATION 的内容,我知道我应该将它与 ACCESS_COARSE_LOCATI
我在我的应用程序中请求 FINE_LOCATION 时遇到了一些问题。我试图阅读一些关于 ACCESS_FINE_LOCATION 的内容,我知道我应该将它与 ACCESS_COARSE_LOCATI
我正在使用 SDK-23,每次运行应用程序时,我的 SecurityException 都会被抛出,调试器的错误如下所示: java.lang.SecurityException: "gps" loc
我最近收到来自 Google 的通知,要求更新我们声明在应用内使用原生地理定位的方式。 在 Ionic (v1) 中,安装了 cordova-plugin-geolocation 2.3.0 “Geo
我正在更新我的应用程序以使用新的 Android Marshmallow 权限框架,看起来用户在运行时授予 ACCESS_FINE_LOCATION 权限就足以使应用程序正常工作。这就是我所做的: p
我正在尝试在 Android(2.2 和 2.3)中使用 GPS,但在尝试使用 LocationManager 对象时出现以下错误: WARN/System.err(522): java.lang.S
向我的 list 文件添加权限时,以下 xml 有效。 但是,这个 xml 不起作用。 我应该使用哪一个?如果是第一个,为什么不工作?我该如何解决? 另外,我收到了一个与 Android 6.0
有很多类似的问题被问到,但似乎没有一个能解决我的问题。 我已经应用了文档中提到的运行时权限,但仍然收到运行时错误 LocationServices.FusedLocationApi.requestLo
这个问题在这里已经有了答案: Android permission doesn't work even if I have declared it (12 个答案) 关闭2 年前。 我在另一个应用程
我想从 NETWORK_PROVIDER 获取准确位置,同时关闭 GPS 以节省电量。这似乎是不可能的,因为 android,当指定 ACCESS_FINE_LOCATION 时,即使没有向 GPS_
我目前正在尝试获取用户当前位置的纬度和经度。我在 list 中启用了以下权限。 TargetSDK和min sdk都是23
我有一个 GPS 应用程序已经在 list 中请求 ACCESS_FINE_LOCATION 权限,现在我想添加一个需要 ACCESS_COARSE_LOCATION 的库 (MoPub)。 我是否正
我设置了beacon后台扫描using this tutorial在 BaseApplication 类中,但在 Marshmallow 运行设备中,它显示此日志: Caught a RuntimeE
我的应用正在尝试访问设备的位置,并且我在 AndroidManifest.xml 中包含了以下内容: 我已经按如下方式实现了 Google
我是一名优秀的程序员,十分优秀!