Android Runtime Permissions in Marshmallow and above version

In Marshmallow the Google introduced Android Runtime Permissions to access the user data. Now the users grant permission to use their data whenever it required. This major approach give a user more control over the App. However a user have an option to revoke permission from the settings. There are two types of Android App Permissions.

1. Normal Permissions: Normal permission don’t affect the privacy of a user directly. If it is granted in AndroidManifest file the system grants automatically on the time of installation. Here are some examples of normal permissions.

2. Dangerous Permissions: The Dangerous Permissions can give App to access the private  data of users. Hence the users has to explicitly permit the App to access their data. Here are some examples of dangerous permissions.

Handling Android App Permission: Let’s check a single request to access the camera of a user. We are using  Button on click of this the App will ask to access Camera.

Step – 1:  Add the following code in AndroidManifest file to access the camera.

Step – 2: Set targetSdkVersion 23 or above in your build.gradle file of App.

Step -3: Add <Button/> in your layout file.

Step – 4: In the given code below we have used checkSelfPermission  to check permission and requestPermissions to request permission to access camera. In spite of these we have also used onRequestPermissionsResult to get a result and the result can be checked using condition of grantResults.

Demo: 
Android Runtime Permission

Android Runtime Permission

Handling Multiple Android Runtime Permissions: We can also add multiple requests on a single click of button.

Step -1 Add permission in AndroidManifest file. Here we are adding only two records however we can also more requests.

Step-2 Create a button on click of that we will ask for multiple request.

Step-3 We can simply add multiple request in requestPermission.

Demo:

I hope this post is helpful. If you are facing any error or have queries Let me know in the comments. You can also check our others helpful tutorials.

  1. Populate a spinner from JSON data.
  2. Login and Signup with PHP MySQL -1
  3. Email and Mobile Number validation in Android

Article Credit: Requesting Permissions at Run Time