Ask Multiple Permissions Android

前端 未结 5 490
小鲜肉
小鲜肉 2020-12-28 11:00

I\'m modifying an existing Face Tracker app Android\'s Facial Recognition sample projects. I\'m having an issue with requesting multiple permanent permissions. The method be

5条回答
  •  攒了一身酷
    2020-12-28 11:30

    Why do you want to request permissions multiple times. The requestpermission method accepts array of Permissions. To request for any permission you can use the below code and add the permissions you need. This is how you handle runtime permissions by requesting them before accessing any data related to permission

    public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
    
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {
    
            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 0:
                boolean isPerpermissionForAllGranted = false;
                if (grantResults.length > 0 && permissions.length==grantResults.length) {
                    for (int i = 0; i < permissions.length; i++){
                        if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
                            isPerpermissionForAllGranted=true;
                        }else{
                            isPerpermissionForAllGranted=false;
                        }
                    }
    
                    Log.e("value", "Permission Granted, Now you can use local drive .");
                } else {
                    isPerpermissionForAllGranted=true;
                    Log.e("value", "Permission Denied, You cannot use local drive .");
                }
                if(isPerpermissionForAllGranted){
                    shoro();
                }
                break;
            }
        }
    

    Once you do that, for devices with API >=23 You will get popup at runtime and then once the user accepts the permission or rejects it, your onRequestPermissionsResult method is called. so here you will have to handle your check whether user granted the app the permission. If yes you can continue with your logic

提交回复
热议问题