Last Updated:

Work with Runtime Permissions in Android. Get permissions programmatically

Runtime Permissions in Android API 23. Theory

One of the major changes in Android API 23 is the new permission system. In earlier versions, permissions were declared in the AndroidManifest file.xml and no further action was required. But with Android 6, we have to request permissions from the "Dangerous" category at runtime.

The documentation for working with permissions indicates that you should not obtain them all at once, but as you need a particular function during the operation of the application.

Before requesting permissions, we need to describe them in the .AndroidManifest.xml

Permitting process:

1). Find out if the application has received the necessary permission. For example:

2). If there is no permission, then request it using the method:requestPermissions()

You can request multiple permissions at once as follows:

3). You can process the result of the permission request as follows:

Note the array — this is where the results for each of the requested permissions are stored. If in the method we requested 1 permission, then we process only 1 value of the result array: .

grantResults[]requestPermissions()grantResults[0]

Some permissions have migrated to the settings section: we still describe them in the manifest file, but you no longer need to process them inside the application, but on the application settings page.

Let's look at an example of different ways to obtain permissions.

Runtime Permissions in Android API 23. Practice

In our test app, we'll be getting phone data, so we need to request permission to receive.

Manifest.permission.READ_PHONE_STATE

In the code below, we describe the entire process of handling so-called "Dangerous" permissions that require user interaction. To get started, create a new project in Android Studio.

Go to the file and add permission to work with calls:

AndroidManifest.xml

Now go to MainActivity.java and describe the process of obtaining permissions and processing the results:

If you run the application, we get the following result:

runtime permissions android api

On the first screen, we ask for permission. 2 — successful receipt of them. 3 - Permission not received

The idea with permissions is that if the user does not allow the application to access something, then we must disable this functionality in the application and work with the rest of the functionality of the application. And what if the requested permission is critical to our application?

For example, working with calls is the basis of the application, and the user has denied access. In this case, at the next launch, the user should be given the opportunity to change his choice, otherwise the application is closed. Let's implement this functionality in our application:

I specifically highlighted the new functionality:

In the code above, we created our own AlertDialog, in which we ask the user to go to the settings and still enable the desired permission, otherwise the application will simply finish working.

Let's run the application on a device with Android (API 23) and see the result:
Result here

The user rejects the permission request, after which the application asks him to go to the settings and still give permission - otherwise it simply closes

The source code of the application is here.

Upgrade your apps to API 23 and above with the Android Runtime Permissions engine. Subscribe to new articles on Android and Java programming!