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 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" ?> package = "org.neveropen.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: