问题
java.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.
The app works fine for Android version 5(Lollipop) but gets a Security Exception on the Android version 6.
回答1:
On your activity:
private static final int REQUEST_FINE_LOCATION=0
...
loadPermissions(Manifest.permission.ACCESS_FINE_LOCATION,REQUEST_FINE_LOCATION);
Implementation:
private void loadPermissions(String perm,int requestCode) {
if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, perm)) {
ActivityCompat.requestPermissions(this, new String[]{perm},requestCode);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// granted
}
else{
// no granted
}
return;
}
}
}
回答2:
This is because these permissions are dangerous permission as per new permission model of android 6.0.
dangerous permissions need to be asked for at run time and should be checked every time before usage.
you will have to change your implementation to adapt new permission model.
来源:https://stackoverflow.com/questions/33063712/security-exception-only-on-android-6