Android 6.0 multiple permissions

后端 未结 22 1875
太阳男子
太阳男子 2020-11-22 03:58

I know that Android 6.0 has new permissions and I know I can call them with something like this

if (ContextCompat.checkSelfPermission(this, Manifest.permiss         


        
相关标签:
22条回答
  • 2020-11-22 04:20

    Checking every situation

    if denied - showing Alert dialog to user why we need permission

    public static final int MULTIPLE_PERMISSIONS = 1;
    public static final int CAMERA_PERMISSION_REQUEST_CODE = 2;
    public static final int STORAGE_PERMISSION_REQUEST_CODE = 3;
    
        private void askPermissions() {
    
        int permissionCheckStorage = ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
    
        int permissionCheckCamera = ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA); 
    
       // we already asked for permisson & Permission granted, call camera intent
        if (permissionCheckStorage == PackageManager.PERMISSION_GRANTED && permissionCheckCamera == PackageManager.PERMISSION_GRANTED) {
    
            launchCamera();
    
        } //asking permission for the first time
         else if (permissionCheckStorage != PackageManager.PERMISSION_GRANTED && permissionCheckCamera != PackageManager.PERMISSION_GRANTED) {
    
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MULTIPLE_PERMISSIONS);
    
        } else {
            // Permission denied, so request permission
    
            // if camera request is denied
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.CAMERA)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("You need to give permission to take pictures in order to work this feature.");
                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                builder.setPositiveButton("GIVE PERMISSION", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
    
                        // Show permission request popup
                        ActivityCompat.requestPermissions(this,
                                new String[]{Manifest.permission.CAMERA},
                                CAMERA_PERMISSION_REQUEST_CODE);
                    }
                });
                builder.show();
    
            }   // if storage request is denied
                    else if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("You need to give permission to access storage in order to work this feature.");
                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                builder.setPositiveButton("GIVE PERMISSION", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
    
                        // Show permission request popup
                        ActivityCompat.requestPermissions(this,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                STORAGE_PERMISSION_REQUEST_CODE);
                    }
                });
                builder.show();
    
            } 
    
        }
    }
    

    Checking Permission Results

     @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
        switch (requestCode) {
            case CAMERA_PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && permissions[0].equals(Manifest.permission.CAMERA)) {
                    // check whether camera permission granted or not.
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        launchCamera();
                    }
                }
                break;
            case STORAGE_PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    // check whether storage permission granted or not.
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        launchCamera();
                    }
                }
                break;
            case MULTIPLE_PERMISSIONS:
                if (grantResults.length > 0 && permissions[0].equals(Manifest.permission.CAMERA) && permissions[1].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    // check whether All permission granted or not.
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                        launchCamera();
    
                    }
                }
                break;
            default:
                break;
        }
    }
    

    you can just copy and paste this code, it works fine. change context(this) & permissions according to you.

    0 讨论(0)
  • 2020-11-22 04:21

    Short and sweet :). what I believe in.

    int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}; // List of permissions required
    
    public void askPermission()
    {
        for (String permission : PERMISSIONS) {
            if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(PERMISSIONS, PERMISSION_ALL);
                return;
            }
        }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
    
        switch (requestCode) {
            case 1:{
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
                    //Do your work.
                } else {
                    Toast.makeText(this, "Until you grant the permission, we cannot proceed further", Toast.LENGTH_SHORT).show();
                }
                return;
            }
        }
    
    0 讨论(0)
  • 2020-11-22 04:23

    Check the "Asking for multiple permissions at a time" section in this article:

    Things you need to know about Android M permissions

    It's very well explained, and may also touch other related topics you haven't think about.

    0 讨论(0)
  • 2020-11-22 04:24

    Just include all 4 permissions in the ActivityCompat.requestPermissions(...) call and Android will automatically page them together like you mentioned.

    I have a helper method to check multiple permissions and see if any of them are not granted.

    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;
    }
    

    Or in Kotlin:

    fun hasPermissions(context: Context, vararg permissions: String): Boolean = permissions.all {
        ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
    }
    

    Then just send it all of the permissions. Android will ask only for the ones it needs.

    // The request code used in ActivityCompat.requestPermissions()
    // and returned in the Activity's onRequestPermissionsResult()
    int PERMISSION_ALL = 1; 
    String[] PERMISSIONS = {
      android.Manifest.permission.READ_CONTACTS, 
      android.Manifest.permission.WRITE_CONTACTS, 
      android.Manifest.permission.WRITE_EXTERNAL_STORAGE, 
      android.Manifest.permission.READ_SMS, 
      android.Manifest.permission.CAMERA
    };
    
    if (!hasPermissions(this, PERMISSIONS)) {
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }
    
    0 讨论(0)
  • 2020-11-22 04:24

    I found this is in the runtime permissions example from Google's github.

     private static String[] PERMISSIONS_CONTACT = {Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS};
    private static final int REQUEST_CONTACTS = 1;
    ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
    
    0 讨论(0)
  • 2020-11-22 04:28

    Simple way to ask multiple permission,

    https://github.com/sachinvarma/EasyPermission

    How to add :

    repositories {
            maven { url "https://jitpack.io" }
        }
    
    implementation 'com.github.sachinvarma:EasyPermission:1.0.1'
    

    How to ask permission:

     List<String> permission = new ArrayList<>();
     permission.add(EasyPermissionList.READ_EXTERNAL_STORAGE);
     permission.add(EasyPermissionList.ACCESS_FINE_LOCATION);
    
     new EasyPermissionInit(MainActivity.this, permission);
    

    For more Details - >

    https://github.com/sachinvarma/EasyPermission/blob/master/app/src/main/java/com/sachinvarma/easypermissionsample/MainActivity.java

    It may help someone in future.

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