安全例外仅适用于Android 6

时间:2015-10-11 09:57:03

标签: android android-6.0-marshmallow

java.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.

该应用适用于Android版本5(Lollipop),但在Android版本6上获得安全例外。

2 个答案:

答案 0 :(得分:16)

这是因为根据android 6.0的新权限模型,这些权限是危险权限。

运行时需要提出危险权限,每次使用前都应该检查。

您必须将实施更改为adapt new permission model

答案 1 :(得分:10)

关于您的活动:

private static final int REQUEST_FINE_LOCATION=0
...
loadPermissions(Manifest.permission.ACCESS_FINE_LOCATION,REQUEST_FINE_LOCATION);

实现:

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

    }

}