Open In App

How to Request Permissions in Android Application?

Last Updated : 30 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Starting from Android 6.0 (API 23), users are not asked for permissions at the time of installation rather developers need to request the permissions at the run time. Only the permissions that are defined in the manifest file can be requested at run time.

Types of Permissions

1. Install-Time Permissions: If the Android 5.1.1 (API 22) or lower, the permission is requested at the installation time at the Google Play Store.

If the user Accepts the permissions, the app is installed. Else the app installation is canceled.

2. Run-Time Permissions: If the Android 6 (API 23) or higher, the permission is requested at the run time during the running of the app.

If the user Accepts the permissions, then that feature of the app can be used. Else to use the feature, the app requests permission again.

So, now the permissions are requested at runtime. In this article, we will discuss how to request permissions in an Android Application at run time. 

Steps for Requesting permissions at run time  

Step 1: Declare the permission in the Android Manifest file: In Android, permissions are declared in the AndroidManifest.xml file using the uses-permission tag. 

<uses-permission android:name=”android.permission.PERMISSION_NAME”/>

Here we are declaring storage and camera permission.

XML




<!--Declaring the required permissions-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />


Step 2: Modify activity_main.xml file to Add two buttons to request permission on button click: Permission will be checked and requested on button click. Open the activity_main.xml file and add two buttons to it.

XML




<!--Button to request storage permission-->
<Button
    android:id="@+id/storage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Storage"
    android:layout_marginTop="16dp"
    android:padding="8dp"
    android:layout_below="@id/toolbar"
    android:layout_centerHorizontal="true"/>
 
<!--Button to request camera permission-->
<Button
    android:id="@+id/camera"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Camera"
    android:layout_marginTop="16dp"
    android:padding="8dp"
    android:layout_below="@id/storage"
    android:layout_centerHorizontal="true"/>


Step 3: Check whether permission is already granted or not. If permission isn’t already granted, request the user for the permission: In order to use any service or feature, the permissions are required. Hence we have to ensure that the permissions are given for that. If not, then the permissions are requested.

Check for permissions: Beginning with Android 6.0 (API level 23), the user has the right to revoke permissions from any app at any time, even if the app targets a lower API level. So to use the service, the app needs to check for permissions every time.

Syntax: 

if(ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR)
   != PackageManager.PERMISSION_GRANTED) 
{
    // Permission is not granted
}

Request Permissions: When PERMISSION_DENIED is returned from the checkSelfPermission() method in the above syntax, we need to prompt the user for that permission. Android provides several methods that can be used to request permission, such as requestPermissions().

Syntax: 

ActivityCompat.requestPermissions(MainActivity.this, 
                                  permissionArray, 
                                  requestCode);

Here permissionArray is an array of type String.

Example:

Java




// Function to check and request permission
public void checkPermission(String permission, int requestCode)
{
    // Checking if permission is not granted
    if (ContextCompat.checkSelfPermission(MainActivity.this, permission) == PackageManager.PERMISSION_DENIED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[] { permission }, requestCode);
    }
    else {
        Toast.makeText(MainActivity.this, "Permission already granted", Toast.LENGTH_SHORT).show();
    }
}


Kotlin




// Function to check and request permission.
private fun checkPermission(permission: String, requestCode: Int) {
        if (ContextCompat.checkSelfPermission(this@MainActivity, permission) == PackageManager.PERMISSION_DENIED) {
            // Requesting the permission
            ActivityCompat.requestPermissions(this@MainActivity, arrayOf(permission), requestCode)
        } else {
            Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT).show()
        }
   }


This function will show a Toast message if permission is already granted otherwise prompt the user for permission.

Step 4: Override onRequestPermissionsResult() method: onRequestPermissionsResult() is called when user grant or decline the permission. RequestCode is one of the parameters of this function which is used to check user action for the corresponding requests. Here a toast message is shown indicating the permission and user action. 

Example:

Java




// This function is called when user accept or decline the permission.
// Request Code is used to check which permission called this function.
// This request code is provided when user is prompt for permission.
@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String[] permissions,
                                       @NonNull int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
   
    if (requestCode == CAMERA_PERMISSION_CODE) {
 
        // Checking whether user granted the permission or not.
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
 
            // Showing the toast message
            Toast.makeText(MainActivity.this, "Camera Permission Granted", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(MainActivity.this, "Camera Permission Denied", Toast.LENGTH_SHORT).show();
        }
    }
    else if (requestCode == STORAGE_PERMISSION_CODE) {
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MainActivity.this, "Storage Permission Granted", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(MainActivity.this, "Storage Permission Denied", Toast.LENGTH_SHORT).show();
        }
    }
}


Kotlin




// This function is called when the user accepts or decline the permission.
// Request Code is used to check which permission called this function.
// This request code is provided when the user is prompt for permission.
override fun onRequestPermissionsResult(requestCode: Int,
                                        permissions: Array<String>,
                                        grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == CAMERA_PERMISSION_CODE) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this@MainActivity, "Camera Permission Granted", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this@MainActivity, "Camera Permission Denied", Toast.LENGTH_SHORT).show()
            }
        } else if (requestCode == STORAGE_PERMISSION_CODE) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this@MainActivity, "Storage Permission Granted", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this@MainActivity, "Storage Permission Denied", Toast.LENGTH_SHORT).show()
            }
     }
}


Below is the complete code of this application:

Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
     
    <!-- To show toolbar-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        app:title="GFG | Permission Example"
        app:titleTextColor="@android:color/white"
        android:layout_height="?android:attr/actionBarSize"/>
 
    <!--Button to request storage permission-->
    <Button
        android:id="@+id/storage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Storage"
        android:layout_marginTop="16dp"
        android:padding="8dp"
        android:layout_below="@id/toolbar"
        android:layout_centerHorizontal="true"/>
 
    <!--Button to request camera permission-->
    <Button
        android:id="@+id/camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Camera"
        android:layout_marginTop="16dp"
        android:padding="8dp"
        android:layout_below="@id/storage"
        android:layout_centerHorizontal="true"/>
 
</RelativeLayout>


Below is the code for the AndroidManifest.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="org.geeksforgeeks.requestPermission">
 
        <!--Declaring the required permissions-->
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.CAMERA" />
 
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
             
            <activity
                android:name=".MainActivity">
 
                <intent-filter>
                    <action
                        android:name="android.intent.action.MAIN" />
 
                    <category
                        android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
               
            </activity>
        </application>
       
</manifest>


Below is the code for the MainActivity file.

Kotlin




import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
 
class MainActivity : AppCompatActivity() {
 
    companion object {
        private const val CAMERA_PERMISSION_CODE = 100
        private const val STORAGE_PERMISSION_CODE = 101
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // Defining Buttons
        val storage: Button? = findViewById(R.id.storage)
        val camera: Button? = findViewById(R.id.camera)
 
        // Set Buttons on Click Listeners
        storage?.setOnClickListener {checkPermission(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    STORAGE_PERMISSION_CODE)
        }
        camera?.setOnClickListener {
            checkPermission(Manifest.permission.CAMERA,
                    CAMERA_PERMISSION_CODE)
        }
    }
 
    // Function to check and request permission.
    private fun checkPermission(permission: String, requestCode: Int) {
        if (ContextCompat.checkSelfPermission(this@MainActivity, permission) == PackageManager.PERMISSION_DENIED) {
 
            // Requesting the permission
            ActivityCompat.requestPermissions(this@MainActivity, arrayOf(permission), requestCode)
        } else {
            Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT).show()
        }
    }
 
    // This function is called when the user accepts or decline the permission.
    // Request Code is used to check which permission called this function.
    // This request code is provided when the user is prompt for permission.
    override fun onRequestPermissionsResult(requestCode: Int,
                                            permissions: Array<String>,
                                            grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == CAMERA_PERMISSION_CODE) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this@MainActivity, "Camera Permission Granted", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this@MainActivity, "Camera Permission Denied", Toast.LENGTH_SHORT).show()
            }
        } else if (requestCode == STORAGE_PERMISSION_CODE) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this@MainActivity, "Storage Permission Granted", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this@MainActivity, "Storage Permission Denied", Toast.LENGTH_SHORT).show()
            }
        }
    }
}


Java




import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    // Defining Buttons
    private Button storage, camera;
 
    // Defining Permission codes.
    // We can give any value
    // but unique for each permission.
    private static final int CAMERA_PERMISSION_CODE = 100;
    private static final int STORAGE_PERMISSION_CODE = 101;
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        storage = findViewById(R.id.storage);
        camera = findViewById(R.id.camera);
 
        // Set Buttons on Click Listeners
        storage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, STORAGE_PERMISSION_CODE);
            }
        });
 
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                checkPermission(Manifest.permission.CAMERA, CAMERA_PERMISSION_CODE);
            }
        });
    }
 
    // Function to check and request permission.
    public void checkPermission(String permission, int requestCode)
    {
        if (ContextCompat.checkSelfPermission(MainActivity.this, permission) == PackageManager.PERMISSION_DENIED) {
 
            // Requesting the permission
            ActivityCompat.requestPermissions(MainActivity.this, new String[] { permission }, requestCode);
        }
        else {
            Toast.makeText(MainActivity.this, "Permission already granted", Toast.LENGTH_SHORT).show();
        }
    }
 
    // This function is called when the user accepts or decline the permission.
    // Request Code is used to check which permission called this function.
    // This request code is provided when the user is prompt for permission.
 
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                        @NonNull String[] permissions,
                                        @NonNull int[] grantResults)
    {
        super.onRequestPermissionsResult(requestCode,
                                        permissions,
                                        grantResults);
 
        if (requestCode == CAMERA_PERMISSION_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this, "Camera Permission Granted", Toast.LENGTH_SHORT) .show();
            }
            else {
                Toast.makeText(MainActivity.this, "Camera Permission Denied", Toast.LENGTH_SHORT) .show();
            }
        }
        else if (requestCode == STORAGE_PERMISSION_CODE) {
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this, "Storage Permission Granted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "Storage Permission Denied", Toast.LENGTH_SHORT).show();
            }
        }
    }
}


Output: 

On starting the application: 

On clicking the camera button for the first time:

On Granting the permission:

On clicking the camera button again:



Similar Reads

Easy Runtime Permissions in Android with Dexter
The Runtime Permission in Android which introduced in Marshmallow and due to these runtime permissions users can grant permission to the app in runtime, also while writing the code for runtime permissions. Sometimes handling the task of requesting permissions becomes so difficult and the developer has to write a huge amount of code. So we will see
7 min read
File Permissions in Java
Java provides a number of method calls to check and change the permission of a file, such as a read-only file can be changed to have permissions to write. File permissions are required to be changed when the user wants to restrict the operations permissible on a file. For example, file permission can be changed from write to read-only because the u
3 min read
How to Open a URL in Android's Web Browser in an Android Application?
It might be essential to launch a browser separately while using an Android application to access a URL. Any action made on Views using Action Listeners has the potential to initiate the task of accessing a URL. In this article, we will learn about How we can Open a URL in Android's Web Browser from an Android Application. For this article, we will
2 min read
How to Make an HTTP Request with Android?
In any Android Application, there is only so much a user can do without an internet connection. All modern Android Applications interact with resources available online or a backend-specific to the app. In this article, we will look at one of the ways through which we can retrieve and post resources online through HTTP Requests. We will use the Vol
6 min read
Android - POST Raw Whole JSON in the Body of a Retrofit Request
REST APIs provide us a functionality with the help of which we can add data to our database using REST API. For posting this data to our database we have to use the Post method of REST API to post our data. We can post data to our API in different formats. In this article, we will take a look at How to Post raw whole JSON in the body of a Retrofit
8 min read
Spring Boot - application.yml/application.yaml File
Spring is widely used for creating scalable applications. For web applications Spring provides. In Spring Boot, whenever we create a new Spring Boot Application in spring starter, or inside an IDE (Eclipse or STS) a file is located inside the src/main/resources folder named as application.properties file which is shown in the below media: So in a s
4 min read
How to Make a Phone Call From an Android Application?
In this article, you will make a basic android application that can be used to call some numbers through your android application. You can do so with the help of Intent with action as ACTION_CALL. Basically Intent is a simple message object that is used to communicate between android components such as activities, content providers, broadcast recei
4 min read
How to Build a TODO Android Application with AWS DynamoDB as Database?
This is a TODO application made on android studio IDE, code is written in JAVA, and data store in AWS DynamoDB and fetch to the home screen. The connection established between the application and AWS through Amplify CLI and the data is successful gets stored by creating a TODO Table in DynamoDB, a Schema is also created which shows the structure of
7 min read
How to Send an Email From an Android Application?
In this article, you will make a basic Android Application that can be used to send email through your android application. You can do so with the help of Intent with action as ACTION_SEND with extra fields: email id to which you want to send mail,the subject of the email andbody of the email.Basically Intent is a simple message object that is used
4 min read
How to Share a Captured Image to Another Application in Android?
Pre-requisite: How to open a Camera through Intent and capture an image In this article, we will try to send the captured image (from this article) to other apps using Android Studio. Approach: The image captured gets stored on the external storage. Hence we need to request permission to access the files from the user. So take permission to access
6 min read
Article Tags :
Practice Tags :