Android | How to Request permissions in Android Application?

Starting from Android 6.0 (API 23), users are not asked for permissions at the time of installation rather developers need to request for 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 cancelled.

  2. Run-Time Permissions: If the Android 6 (API 23) or higher, the permission is requested at the run time during the runnnig 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 the 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 :

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

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

    Here we are declaring storage and camera permission.

    AndroidManifest.xml

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!--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" />

    chevron_right

    
    

  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 activity_main.xml file and add two buttons in it.

    activity_main.xml

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!--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"/>

    chevron_right

    
    

  3. Check whether permission is already granted or not. If permission isn’t already granted, request 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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();
        }
    }

    chevron_right

    
    

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

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

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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();
            }
        }
    }

    chevron_right

    
    

Below is the complete code of this application:

AndroidManifest.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>

chevron_right


MainActivity.java

filter_none

edit
close

play_arrow

link
brightness_4
code

package org.geeksforgeeks.requestPermission;
  
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();
            }
        }
    }
}

chevron_right


activity_main.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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>

chevron_right


Output:

  • On starting the application:

  • On clicking camera button for first time:

  • On Granting the permission:

  • On clicking camera button again:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.