我最近在Android版6.0的设备上发生了一个问题
我遇到了以下崩溃
java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
at android.os.Parcel.readException(Parcel.java:1627)
at android.os.Parcel.readException(Parcel.java:1579)
at ub.a(:com.google.android.gms.DynamiteModulesB:1191)
at to.a(:com.google.android.gms.DynamiteModulesB:2110)
at lw.b(:com.google.android.gms.DynamiteModulesB:148)
at ml.b(:com.google.android.gms.DynamiteModulesB:1164)
at nf.b(:com.google.android.gms.DynamiteModulesB:260)
at na.b(:com.google.android.gms.DynamiteModulesB:324)
at tn.a(:com.google.android.gms.DynamiteModulesB:78)
at maps.af.L.a(Unknown Source)
at na.a(:com.google.android.gms.DynamiteModulesB:13201)
at mo.g(:com.google.android.gms.DynamiteModulesB:499)
at mo.a(:com.google.android.gms.DynamiteModulesB:435)
at mh.a(:com.google.android.gms.DynamiteModulesB:1437)
at pj.a(:com.google.android.gms.DynamiteModulesB:227)
at oz.a(:com.google.android.gms.DynamiteModulesB:780)
at oq.a(:com.google.android.gms.DynamiteModulesB:1829)
at ot.handleMessage(:com.google.android.gms.DynamiteModulesB:5339)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
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)
任何人都可以帮忙吗?
答案 0 :(得分:8)
以下是可用于请求运行时权限的示例代码:
private static final int MY_PERMISSIONS_REQUEST_READ_FINE_LOCATION = 100;
和
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation 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_PERMISSION_REQUEST_READ_FINE_LOCATION);
// MY_PERMISSION_REQUEST_READ_FINE_LOCATION 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_PERMISSION_REQUEST_READ_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
}
}
您可以在活动中轻松调用此功能,但我强烈建议您在此处阅读documentation以获得更好的主意。
我希望这有帮助!
答案 1 :(得分:4)
如果这些权限已经在AndroidManifest.xml文件中,请在应用级build.gradle文件中将targetSdkVersion
设置为 22 ,如下所示...
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.app"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName '1'
}
}
注意运行时权限始终是首选方法。
答案 2 :(得分:3)
您的Android 6用户可以有选择地撤销您的应用程序的权限,因此您必须在运行时请求权限。
来自the documentation的示例:
private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111;
// ...
// Check for permission
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this, // Activity
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_FINE_LOCATION);
}
}
// Get permission result
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_FINE_LOCATION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
} else {
// permission was denied
}
return;
}
}
}