VOOZH about

URL: https://www.geeksforgeeks.org/android/android-how-to-request-permissions-in-android-application/

⇱ How to Request Permissions in Android Application? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Request Permissions in Android Application?

Last Updated : 9 Apr, 2025

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.

👁 Image

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.

👁 Image

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.


Step 2: Modify activity_main.xml file

Add a button 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.

activity_main.xml:


Step 3: Working with MainActivity file

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.
  • 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().
  • 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. 


Output:


Comment
Article Tags:
Article Tags:

Explore