Before the release of Android 6.0 Marshmallow, the Android apps used to take all kinds of needed permission while downloading the app but after releasing the Android Marshmallow, the new interface for asking permissions has introduced.
In this new method, users have to give the app permission while opening the app[ After installation ]. Or while accessing some features of the app.
Why do Android apps ask for permissions?
Apps ask your permission because they need it to operate app functionalities. Apps can't access users' data without the user's permission.
Types of Android Permissions
Normal Permission
Normal permission is a permission that does not affect user privacy or user data. Following permissions are classified as Normal Permission:
- ACCESS_LOCATION_EXTRA_COMMANDS
- ACCESS_NETWORK_STATE
- ACCESS_NOTIFICATION_POLICY
- ACCESS_WIFI_STATE
- BLUETOOTH
- BLUETOOTH_ADMIN
- BROADCAST_STICKY
- CHANGE_NETWORK_STATE
- CHANGE_WIFI_MULTICAST_STATE
- CHANGE_WIFI_STATE
- DISABLE_KEYGUARD
- EXPAND_STATUS_BAR
- GET_PACKAGE_SIZE
- INSTALL_SHORTCUT
- INTERNET
- KILL_BACKGROUND_PROCESSES
- MODIFY_AUDIO_SETTINGS
- NFC
- READ_SYNC_SETTINGS
- READ_SYNC_STATS
- RECEIVE_BOOT_COMPLETED
- REORDER_TASKS
- REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
- REQUEST_INSTALL_PACKAGES
- SET_ALARM
- SET_TIME_ZONE
- SET_WALLPAPER
- SET_WALLPAPER_HINTS
- TRANSMIT_IR
- UNINSTALL_SHORTCUT
- USE_FINGERPRINT
- VIBRATE
- WAKE_LOCK
- WRITE_SYNC_SETTINGS
Dangerous permissions
Runtime permissions are also known as dangerous permissions. Permissions that can access the user's data privacy or the device's operation are called Dangerous permissions. The users can allow or deny those permissions.
Accessing the camera, location, microphone, contacts, storage, and sensors are examples of Dangerous permissions. Following permissions are classified as Dangerous Permissions:
- READ_CALENDAR
- WRITE_CALENDAR
- CAMERA
- READ_CONTACTS
- WRITE_CONTACTS
- GET_ACCOUNTS
- ACCESS_FINE_LOCATION
- ACCESS_COARSE_LOCATION
- RECORD_AUDIO
- READ_PHONE_STATE
- READ_PHONE_NUMBERS
- CALL_PHONE
- ANSWER_PHONE_CALLS
- READ_CALL_LOG
- WRITE_CALL_LOG
- ADD_VOICEMAIL
- USE_SIP
- PROCESS_OUTGOING_CALLS
- BODY_SENSORS
- SEND_SMS
- RECEIVE_SMS
- READ_SMS
- RECEIVE_WAP_PUSH
- RECEIVE_MMS
- READ_EXTERNAL_STORAGE
- WRITE_EXTERNAL_STORAGE
- ACCESS_MEDIA_LOCATION
- ACCEPT_HANDOVER
- ACCESS_BACKGROUND_LOCATION
- ACTIVITY_RECOGNITION
So it was the brief information about Permissions in Android, now let's come to the main topic which is how to show permission dialog in the android app?
In this article, we'll create a simple app and implement a storage permission dialog in it. So let's start creating it.
Android storage permission dialog 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.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private final int STORAGE_PERMISSION_CODE = 1;
@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 {
requestPermission();
}
}
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(this)
.setTitle("Permission needed")
.setMessage("This permission is needed to access storage data")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create().show();
} else {
ActivityCompat.requestPermissions(this,
new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
}
// To Perform any action while user clicks on allow or deny button
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
requestPermission();
}
}
}
}Code Explanation :
When the app starts, the first thing to check is whether the user has given permission or not. So that check the storage permission using ContextCompat.checkSelfPermission in the onCreate() method. And if there is no permission then, ask for storage permission from the user.
In order to ask permission, there is a method defined in the code-named requestPermission( )
In the requestPermission( ) method, first, check if the user had declined the permission dialog before? if yes then show the alert dialog and tell the user that, "the app will require permission". And if the user had not declined the permission dialog before it means the user has opened the app for the first time so we have to show him a permission dialog.
In the alert dialog, if the user clicks on the ok button then, display again the permission dialog.
Override the method named onRequestPermissionsResult( ) .
Basically, this method will call when the user allows or denied the permission dialog. So we can perform different actions as the result of allow and deny button.
If you don't know to implement alert dialog in android, then you can learn it from here.
Output
We had implemented an alert dialog so when the permission is denied, an alert dialog is showing, and if the user clicks the ok button of the alert dialog, the permission dialog will re-display.
So that's it for this tutorial, almost all concept of the Android Permission has been covered in this article. In this way, we can display the storage permission dialog in android.
Post a comment if you are facing difficulties to implement Glide and I will answer it as soon as possible.
And don't forget to follow npxcoding on Instagram.
Happy Learning!
Share this article with your friends ✌!!
0 Comments