Ask Multiple Permissions Android

前端 未结 5 488
小鲜肉
小鲜肉 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:20

    I faced the issue today and answer from Aman Grover actually worked for me, you simple need to do following

    final String[] permissions = new String[]{ 
        android.Manifest.permission.READ_CONTACTS, 
        Manifest.permission.READ_EXTERNAL_STORAGE
    };
    
    ActivityCompat.requestPermissions(this, permissions, 123);
    
    0 讨论(0)
  • 2020-12-28 11:26

    This is the simplest solution:

    Add this code in your MainActivity's onCreate method

    //Requesting permissions
            int PERMISSION_ALL = 1;
            String[] PERMISSIONS = {
                    android.Manifest.permission.READ_CONTACTS,
                    android.Manifest.permission.RECORD_AUDIO,
                    android.Manifest.permission.CALL_PHONE,
            };
    
            if (!hasPermissions(this, PERMISSIONS)) {
                ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
            }

    Now, add this function in your main activity class

    public static boolean hasPermissions(Context context, String... permissions) {
        if (context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }
    

    You can add ass many permissions you need to ask at the application startup.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-28 11:31

    you should have only one String array if you want to ask all permissions in one dialog box, like this:

    int ALL_PERMISSIONS = 101;
    
    final String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    
    ActivityCompat.requestPermissions(this, permissions, ALL_PERMISSIONS);
    
    0 讨论(0)
  • 2020-12-28 11:43

    What you have to do is just put the permission sin a String array like

    int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {
            android.Manifest.permission.READ_CONTACTS,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION
    };
    

    and to Check the permission use then

    public static boolean hasPermissions(Context context, String... permissions) {
        if (context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }
    

    Now the Last thing to do finally is use it in oncreate() or onStart()

    if(!hasPermissions(this, PERMISSIONS)){
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        }
    
    0 讨论(0)
提交回复
热议问题