Hello developers, the complete article of Android Runtime Permissions with permission dialog example is already available on this site you can check it out here.
But in this tutorial, we are going to implement a storage permission dialog using the Dexter library.
What is dexter?
Dexter is an Android library that simplifies the process of requesting permissions at runtime. You can read the official documentation here.
Why dexter?
We can ask for runtime permission android using ActivityCompat.requestPermissions so, why we should use the dexter library?
It's because using the Dexter library, we easily implement Easy Runtime Permissions with few lines of code and it reduces boilerplate code.
We are going to build a simple app and will implement Runtime Storage Permissions in it. So let's make it.
Android storage permission dialog using Dexter example
Now open the Android Studio and create a new project or open your existing project. There is nothing to add in layout design so let's move to the manifest part.
Manifest File
To work with any kind of permission in android, we have to define that permission in the Manifest file.
We are going to access storage permission in the app. So add the below lines of code in your project's AndroidManifest.xml file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />AndroidManifest.xml complete code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.simpleapp">
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>
<!--add below 3 lines-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SimpleApp">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>Java code
Open the MainActivity.java file and add the below code:
package com.example.simpleapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "You have already granted this permission!",
Toast.LENGTH_SHORT).show();
} else {
checkPermission();
}
}
private void checkPermission() {
Dexter.withContext(MainActivity.this)
.withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
//Code when Permission granted
Toast.makeText(MainActivity.this, "PERMISSION GRANTED!!", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
//Code when Permission denied
Toast.makeText(MainActivity.this, "PERMISSION DENIED!!", Toast.LENGTH_SHORT).show();
//Send user to settings
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
//Code to show again permission dialog if the user had denied permission in past
permissionToken.continuePermissionRequest();
}
}).check();
}
}Code Explanation :
When the app starts, the first check is the permission already granted or not using ContextCompat.checkSelfPermission
And if it is not granted then, ask for permission, there is a method for this named checkPermission( ).
There are three methods override in this method and those are:
- onPermissionGranted ( )
- onPermissionDenied( )
- onPermissionRationaleShouldBeShown( )
- Add the code which you want to execute when the storage permission is allowed is inside the onPermissionGranted( ) method.
- Add the code which you want to execute when the storage permission is allowed is inside the onPermissionDenied( ) method.
- If the user had denied the permission dialog in the past then, you can reshow the permission dialog inside onPermissionRationaleShouldBeShown( ) using permissionToken.
Output
As you can see, the storage permission dialog is showing when the starts for the first time. And if the user denies the permission then, the device's settings will open in order to give permission manually.
And if the user allows permission, further tasks of the app will be performed.
Dexter multiple permission example
We can also ask for multiple permissions at a time using Dexter library here is the example:
Dexter.withContext(MainActivity.this)
.withPermissions(Manifest.permission.CAMERA,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.READ_CONTACTS)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport multiplePermissionsReport) {
Toast.makeText(MainActivity.this, "All the permissions are granted..", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> list, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
});
}that's it for the tutorial, i hope you got help from this tutorial and share this article with your friend.
And don't forget to follow npxcoding on Instagram.
Happy Learning!
0 Comments