Thursday 22 February 2018

Android Runtime Permissions with Dexter Library (Simple Approach)

App Permission with Dexter Library

Android Runtime Permission With Dexter Library


Implementing Android runtime permissions intoduces in Mashmallow is a hard process and developer needs to write lot of code just to get a single permission.
User has to allow or deny any permission at runtime.

We are going to Use Dexter library to   simplify the process of adding the runtime permissions . 


With Dexter Library , we can have get "Single" runtime permission as well as "Multiple" runtime permission with just a
few lines of code .

You can Download the Project from Github link :

DOWNLOAD PROJECT

or 

https://github.com/jaspreet1990/runtime-permission-dexter-library


Below the are steps to implement Dexter Library 

Step 1 :
Add Library in Gradle :
dependencies {
    
    implementation 'com.karumi:dexter:4.2.0'    // Dexter runtime permissions

}

Step 1 a :

Add code on button click to use Dexter Library  for single Permission:

  Dexter.withActivity(this)
                        .withPermission(Manifest.permission.CAMERA)
                        .withListener(new PermissionListener() {
                            @Override public void onPermissionGranted(PermissionGrantedResponse response)
                            {
                                Toast.makeText(MainActivity.this,"Permession Granted with Dexter",Toast.LENGTH_SHORT).show();
                            }
                            @Override public void onPermissionDenied(PermissionDeniedResponse response)
                            {
                                Toast.makeText(MainActivity.this,"Permession Deny",Toast.LENGTH_SHORT).show();
                               if(response.isPermanentlyDenied()){
                                   // navigate to app setting

                                   Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                                           Uri.fromParts("package", getPackageName(), null));
                                   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                   startActivity(intent);
                               }

                            }
                            @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token)
                            {
                                    token.continuePermissionRequest();
                            }
                        }).check();


Step 1 b :

Add code on button click to use Dexter Library  for Multiple Permission:

   Dexter.withActivity(this)
                        .withPermissions(
                                Manifest.permission.CAMERA,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE
                        )
                        .withListener(new MultiplePermissionsListener() {
                    @Override public void onPermissionsChecked(MultiplePermissionsReport report) {
                        if (report.areAllPermissionsGranted()) {
                            // do you work now
                        }
                        for (int i=0;i<report.getDeniedPermissionResponses().size();i++) {
                            Log.d("dennial permision res", report.getDeniedPermissionResponses().get(i).getPermissionName());
                        }

                        // check for permanent denial of any permission
                        if (report.isAnyPermissionPermanentlyDenied()) {
                            // permission is denied permenantly, navigate user to app settings

                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                                    Uri.fromParts("package", getPackageName(), null));
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                        }
                    @Override public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token)
                    {
                        token.continuePermissionRequest();
                    }
                }).check();


Download the Full Code from here  
or 

https://github.com/jaspreet1990/runtime-permission-dexter-library









0 comments:

Post a Comment