In what cases and for what can Runtime Permissions be used in Android systems?
19 minute(s) read
Published on: May 11, 2021
Updated on: Dec 14, 2021
When installing different types of applications to approve or reject them if necessary, these permissions are for accessing different types of resources in the system and devices. For the application to work well, it must obtain approvals from the user to access the system's resources and the device. Android 6 or higher or Marshmallows are different (runtime permissions android 6). To learn about the different types of access permissions in different versions of the Android operating system, it is recommended that you follow this tutorial article.
What is Runtime-Permission?
Runtime Permission is permissions to access various resources on the device and system that applications need to ask them during installation and the user to determine their access to system and device resources. Permissions for access to various resources of the system and device, such as access to contacts, etc. In other words, it can be said that applications need some resources to work properly and perform all the desired operations. Have access to the device and operating system. This is why when installing applications, a list is displayed that includes access to the device resources, and they must approve or reject it if necessary.
In versions of Android less than six or Android before Marshmallow, when installing applications, a list of device and system resources was displayed to users and was asked to approve or reject them to complete the installation or non-installation process. To take. The list that was displayed for approval or rejection contained all the resources in one place and had to approve or reject them at the same time, which would be a problem:
1- The first case is that had two options to choose from. Either had to approve all the resources displayed in the list to install the application or reject them all. In this case, the application would not be installed. Consider, for example, that of all the resources displayed to the user, does not want to approve one of those resources and does not want the application to access that resource, in which case he had no choice or all of them should be Approved. The application installation process was done, or you had to reject all of them and ignore the application installation.
2- The second problem that users faced in this case is that they do not read the list carefully because the list displayed is long, and all the resources displayed in the list are long and may confirm it. And then get into trouble. For example, suppose a list of resources is displayed to obtain a license and approved it in a hurry without paying attention to it. The application is installed, then it may be due to inaccuracy and paying attention to the resources and the list displayed by the user will face problems. The information and data of some of their sources will face problems.
To solve such problems, when building and presenting a new version of the Android Marshmallow operating system, they set an option for it, Runtime Permission, which does not display long lists when installing applications to obtain permissions, so that the user can easily and accurately Review sources and approve or reject them if necessary.
After presenting this option in the new versions of the systems, users did not encounter long lists that included access resources when installing applications. Still, all the resources were shown to the user one by one to be patient and accurate. Approve or reject the abundance of those resources. Implementing these permissions during installation is different from the versions before Marshmallow. In the versions before Marshmallow, we put all the permissions and resources in the form of a list in the application and project manifest, and they are also given to. Were displayed, but in this case and later versions of Marshmallow, we have to apply some settings to have more control over them. Of course, satisfaction should be a priority for application designers and developers.
runtime-authority in android
To better understand, it is better to consider an application on a virtual network such as Twitter or Instagram. You did not ask for permission to access the resources when installing them, and you easily installed them. But if you want to share an image of the gallery in those apps, you will be presented with a message to confirm or deny the gallery resource access permission. That message will ask you to deny the app access to your image and gallery. Do. In another example, consider the access of these applications to the camera source. If you want to use these applications for taking photos or video recordings, you will immediately encounter an access permission message. The advantage of this type of access permit is that it does not involve the enjoyer with many resources during installation and easily performs the installation process. If the enjoyer wants to use the application, the application also needs access to resources. I will receive access permission from this.
How many accesses are there in Android?
In Android systems, access to resources is divided into two categories during the installation process. - Normal
- Normal Normal permissions are those permissions that are automatically and without the need for approval or rejection of this when installing the application by the operating system to the application. The application can access those resources without obtaining approval from this. Such as Bluetooth access, internet and ...
Below is a list that includes normal permissions. You can check the list below to find out about normal permissions that are automatically given to the application by the operating system.
Dangerous licenses are those licenses that have a high level of security that must be approved or rejected by the user and cannot continue the process without rejecting or approving them, such as camera access permissions (android runtime permissions camera), contacts, etc.
The following is a list of resources that have Dangerous access permissions and must be approved or rejected by the user:
- WRITE_EXTERNAL_STORAGE Note: Normal permissions or dangerous permissions are all specified in the AndroidManifest.xml section, and their definition and location do not differ. The operating system automatically assigns normal permissions, but the user must approve or deny dangerous permissions. It should be noted that the type of licenses is different, but the place of their definition and determination is not different.
How to create a project to receive approval from the user
In this part of the tutorial, we want to create a project that shows the user access permissions and asks them to approve or reject them.
runtime authority machine example
1- We create a project in Android Studio and select the desired name. The name selected in this project is Runtime Permission.
2- After creating the project, you must create an Empty Activity.
3- It should be noted that the language used for this project is Java. It is better to choose the Java language.
4- Create a Button in the activity layout.
5- The codes that should be in the activity_main.xml section are as follows:
machine authority best practices
6- In this project, we want to get the license to access the camera from the user. For this reason, we put the license to access the camera in the manifest part of the program.
7- The codes that should be in the AndroidManifest.xml section are as follows:
8- Define the setOnClickListener event and set a condition for it.
9- The codes that should be in the MainActivity.java section are as follows:
By entering the above codes, we cause it to check whether the permission to access the camera has already been approved by the user or not. If not, the requestCameraPermission method will be executed. Otherwise, it will display a Toast message for the user. The toast displayed to the user will be shown when they have confirmed, and of course, the content of this message will be "Already accessed."
We use the checkSelfPermission method to check if it is active. This condition will always be checked and displayed to the user because the user may have disabled it manually through the program and device settings.
The checkSelfPermission method has two parameters, the first parameter is context and the second parameter is permission.
We define permissions as follows:
The checkSelfPermission method returns two values after checking: PERMISSION_GRANTED: Returns this value when the license has already been approved and approved by the user. PERMISSION_DENIED: This value is returned by the method when the user has been denied access. This value is for situations where the user has been denied access.
10- Press the alt + enter key combination on the requestCameraPermission method to automatically add to the active part. Doing so will automatically add to the active part without writing.
11- The method is created inside the MainActivity class and after the onCreate method.
Like the following:
12- Then, we have to complete the method as follows:
In this section, we must receive confirmation from the user to access the camera source. Before we can send access permissions to the user, we need to make some adjustments to improve the UX.
We use the shouldShowRequestPermissionRationale method and define a condition.
Consider a situation where, for example, a user wants to use a social application and use it to capture an image. In this case, the application sends permission to access the camera source for the user, and the user, without paying attention to it and knowing what this message is for, inadvertently or unknowingly rejects it. In this case, the user can no longer access the camera through a social application such as Instagram and use it to take photos or record video.
What is the solution in this case?
In this case, the shouldShowRequestPermissionRationale method starts.
Using this method, we cause that if the user has previously denied permission to access a source when reusing that resource, a message will be displayed to the user that contains more information, in which case he must give the user an explanation to SionRationale user In the part where we entered the conditional expression, if the value of true is returned or in other words, it is specified that needs further explanation, the first part of the condition and otherwise the second part of the condition will be done. In the first part of this condition, which will be executed if true is returned, we have defined an AlertDialog that displays a brief description for the user. After that, two buttons are displayed for the user again with the titles "I agree" and "Cancel," and the user can select one of these two buttons based on the displayed explanation.
The re-permission method will run when the user selects the Agree button, and if the user selects the next button, the dialog will close, and no action will be taken.
13- We must define the access permit in re-permission.
14- Press the alt + enter key combination on one of them and add it to the activity.
15- Then, we add the following codes to them:
The request Permissions method is used to request permission from the user. This method has three parameters:
- The first parameter:
It is the activity class.
- The second parameter:
Is the permission defined in the String?
- The third parameter:
Is the code used to check the results?
16- The complete codes that should be in the MainActivity section are as follows:
Note: The value to be specified for CAMERA_REQUEST_CODE is optional.
17- To manage the result, we use the onRequestPermissionsResult method.
18- Set the onRequestPermissionsResult method to Override activity:
19- We complete the method as follows:
Then inside the condition is another condition that checks if the length of the grant results is greater than zero. And the license application is also approved. The message "License approved" will be executed if the condition is met. Otherwise, the message "License denied" will be executed.
20- We will implement the project.
21- If we run the project and click on the button, we will see that dialogue and text are displayed along with the camera icon.
22- This dialog has two buttons to deny or confirm the access license, and we can reject or confirm it according to our needs.
23- In this tutorial, we click on ALLOW.
24- Then the permission to access the camera application is issued, and a Toast message is displayed along with it.
25- Then click the button again.
26- We will see that the message "Permission already received" is displayed.
27- In the application management section, access permissions of all resources are shown.
28- Click on Permissions.
29- You will then see that the CAMERA access permit is displayed, and from there, you can also disable the access permit.
30- After performing these steps, we will run the project again from the beginning, and in this part, we must select the DENY button to examine it.
31- If we do this, the message "License denied" will be displayed.
32- Double click on the button.
33- In this case, the dialog is not displayed, but the AlertDialog is executed.
34- If we click on the "I agree" option, the license request will be made again.
35- Of course, this section is different from the previous section, in which the Never ask again option has been added.
36- The complete code that should be in the MainActivity.java section is as follows:
How to create a concurrent permissions project in permission
In the previous project that we created and reviewed together, we had a request from the enjoyer.
But in this project, we want to create a project that has several permission requests from the enjoyer at the same time. In this project, we review and receive three gain permissions together.
In this section, we also intend to add Never ask Again and apply settings on it in such a way that by selecting this option by the enjoyer, the enjoyer will be taken to the device and system settings page to be confused and can easily read Understand and take the necessary measures for it.
1- Open a new project in Android Studio and name its Multiple Runtime Permission as desired.
2- First, we have to add the permissions we consider to the project's manifest part.
3- The codes that should be in the AndroidManifest.xml section are as follows:
The permissions that we added to the project manifest by entering the above code are as follows:
Permission to gain the camera source, permission to gain and read from the memory card, permission to write to the memory card.
4- We complete an activity like the following.
5- The code that should be in the MainActivity.java section is as follows:
In the code above, we define a variable of type int and set the desired value for it as 100, and this variable is used to check the result of the license request.
Then, to define the gain permissions, we create a variable of type String called required Permissions.
A setOnClickListener for the button inside the onCreate method defines an activity that sets a condition using the checkSelfPermission method.
Because we want to check the three conditions separately, we must also write the checkSelfPermission method separately for each license:
In the codes above, we say that the request app permissions method will be executed if the first license, the second license, or the third license is not approved. Otherwise, it will display the message "previously received the license."
Put the alt + enter keys on request app permissions and add the method to the activity this way.
Then we called the shouldShowRequestPermissionRationale for each of the permissions and inserted an AlertDialog if the condition is met, and the re-permission method is executed by clicking "Agree." Otherwise, re-permission is executed.
In the code above, using request Permissions, we request all of this that we have already defined as required from this.
6- Then, we have to add onRequestPermissionsResult to Active. Like the following:
First, the requested code and the result code are checked in the codes listed above. We have defined a boolean type inside this condition called all permissions granted and a false value. Then we define and create a for loop to check all the items, and if this approves all the items, the value of all permissions granted is equal to the value of true, and otherwise, it is equal to the value of false.
Then we need to define a condition and check if all permissions granted are true. If its value is true, display a message with the content "permissions approved" and otherwise a message with the content "not approved."
7- We will implement the project.
8- After executing the project, the dialogue will be displayed if we click on the button.
9- After execution, we will see that this has been asked for permission to gain the camera, which we confirm.
10- After that, they will be asked to gain the memory card. We must also approve and agree.
11- After that, we are faced with the message "Permits approved."
12- After we change this, we click on the button again.
13- After clicking on the button, we will see the message "Permission already received."
14- After this, we have to check the next case, which is when this does not approve.
15- We will run the project again.
16- Click on the DENY button.
17- Then click on the button.
18- AlertDialog is running because we did not approve this once.
19- Select the Never ask again option again and DENY.
20- After this, no license requests will be shown because they have been banned from displaying them.
21- The complete code that should be in the MainActivity.java section is as follows:
In the code above, we create an int variable named REQUEST_PERMISSION_SETTINGS and set the desired value to 101.
We then define an instance of the SharedPreferences object with the desired permission Status name.
22- Put the following code in onCreate:
23- Then add another if to the condition defined in the request app permissions method:
24- Pay attention to the code below:
In this code, we specify that the condition will be fulfilled if any of the license applications are not approved.
Then in the code below, we create an AlertDialog that explains to this that he must enter all the gain manually:
In this article, you will get acquainted with all the tutorials on how to create and request approval permissions when installing the application. If you follow all the steps in this article carefully, you can easily create two different projects.About our Android and iOS app development services