Android onRequestPermissionsResult grantResults size > 1

前端 未结 4 1009
庸人自扰
庸人自扰 2020-12-18 03:51

After requesting permission, the ActivityCompat.OnRequestPermissionsResultCallback sometimes contains multiple grantResults, is it safe to just check the first one?

4条回答
  •  再見小時候
    2020-12-18 04:10

    No, It is not a good way to just check first permission, it might be possible that user have allowed first permission but denied for rest permissions. Here is function i am sharing to check whether all permissions are granted or not

    public boolean hasAllPermissionsGranted(@NonNull int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }
    

    and in your onRequestPermissionsResult

    if(hasAllPermissionsGranted(grantResults)){
        // all permissions granted
    }else {
        // some permission are denied.
    }
    

提交回复
热议问题