Security Exception only on Android 6

帅比萌擦擦* 提交于 2019-12-29 04:18:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!