Security Exception only on Android 6

前端 未结 2 1932
滥情空心
滥情空心 2020-12-14 21:23
java.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.

The app

相关标签:
2条回答
  • 2020-12-14 21:34

    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;
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-14 21:38

    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.

    0 讨论(0)
提交回复
热议问题