Android Easy Runtime Permissions with Dexter
Android Easy Runtime Permissions with Karumi/Dexter :
1. Dexter Permissions Library :dependencies dependencies { // Dexter runtime permissions implementation 'com.karumi:dexter:4.2.0'}1.1 Requesting Single Permission :To request one permission, you'll use withPermission() methodology by passing the desired permission.
You also want a PermissionListener recall to receive the state of the permission.
> onPermissionGranted() will be called once the permission is granted.> onPermissionDenied() will be called when the permission is denied.Here you will check whether or not the permission is for good denied by mistreatmentresponse.isPermanentlyDenied() condition.
The below code requests CAMERA permission:
Dexter.withActivity(this) .withPermission(Manifest.permission.CAMERA) .withListener(new PermissionListener() { @Override public void onPermissionGranted(PermissionGrantedResponse response) { // permission is granted, open the camera } @Override public void onPermissionDenied(PermissionDeniedResponse response) { // check for permanent denial of permission if (response.isPermanentlyDenied()) { // navigate user to app settings } } @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) { token.continuePermissionRequest(); } }).check();
To request multiple permissions at an equivalent time, you will use withPermissions() methodology.
Below code requests STORAGE and LOCATION permissions.
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION)
Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(new MultiplePermissionsListener() {
.withListener(new MultiplePermissionsListener() {
@Override
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
public void onPermissionsChecked(MultiplePermissionsReport report) {
// check if all permissions are granted
// check if all permissions are granted
if (report.areAllPermissionsGranted()) {
if (report.areAllPermissionsGranted()) {
// do you work now
// do you work now
}
}
// check for permanent denial of any permission
if (report.isAnyPermissionPermanentlyDenied()) {
if (report.isAnyPermissionPermanentlyDenied()) {
// permission is denied permenantly, navigate user to app settings
// permission is denied permenantly, navigate user to app settings
}
}
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
token.continuePermissionRequest();
}
}
})
})
.onSameThread()
.onSameThread()
.check();
.check();
You can conjointly catch any errors occurred whereas desegregation the library mistreatmentPermissionRequestErrorListener.
You can conjointly catch any errors occurred whereas desegregation the library mistreatmentPermissionRequestErrorListener.
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(listener)
.withErrorListener(new PermissionRequestErrorListener() {
@Override
public void onError(DexterError error) {
Toast.makeText(getApplicationContext(), "Error occurred! " + error.toString(), Toast.LENGTH_SHORT).show();
}
})
.check();
Dexter.withActivity(this) .withPermissions( Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION) .withListener(listener) .withErrorListener(new PermissionRequestErrorListener() { @Override public void onError(DexterError error) { Toast.makeText(getApplicationContext(), "Error occurred! " + error.toString(), Toast.LENGTH_SHORT).show(); } }) .check();
ow let’s see the way to use dextral in associate example project.
2. Creating New Project1. produce a replacement project in mechanical man Studio from File ⇒ New Project and choose Basic Activity from templates.
2. Add Dexter dependency to your build.gradle
Dexter.withActivity(this) .withPermissions( Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION) .withListener(new MultiplePermissionsListener() { @Override public void onPermissionsChecked(MultiplePermissionsReport report) { // check if all permissions are granted if (report.areAllPermissionsGranted()) { // do you work now } if (report.isAnyPermissionPermanentlyDenied()) { // permission is denied permenantly, navigate user to app settings } } public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) { token.continuePermissionRequest(); } }) .onSameThread() .check();
You can conjointly catch any errors occurred whereas desegregation the library mistreatmentPermissionRequestErrorListener.
Dexter.withActivity(this) .withPermissions( Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION) .withListener(listener) .withErrorListener(new PermissionRequestErrorListener() { @Override public void onError(DexterError error) { Toast.makeText(getApplicationContext(), "Error occurred! " + error.toString(), Toast.LENGTH_SHORT).show(); } }) .check();dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' // ... // Dexter runtime permissions implementation 'com.karumi:dexter:4.2.0'}3. Open the layout file of your main activity (activity_main.xml and content_main.xml) and add 2 buttons to check completely different permission ways. |
No comments