About Android Permissions 📱
working with android permissions,request,response..
What is Android Permissions
Android Permissions are the Token for accessing the System data or features to the apps in the android environment.for the security concern each apps request to the android system for its needed permissions like using CAMERA,CONTACT,LOCATION etc.. Android apps are operates in a Process Sandbox so the apps must explicitly request access to resources and data outside their Sandbox and they request this access by declaring permissions what they need for additional capabilities not provided by the basic Sandbox. Requesting Permissions in the Marshmallow(Android 6.0 — API level 23) devices or higher version’s at run time so the user can revoke the permission at any time and the Pre Marshmallow(Android 5.1 — API level 22) devices can requesting permissions only when the user installs the app,and the only way to revoke the permissions is by uninstalling the app.
Android Permissions Design Pattern
Declaring Permissions
if an app needs to use resources or data outside of its own process sandbox,the app has to request the appropriate permission. You declare that your app needs a permission by listing the permission in the App Manifest.To declare that your app needs a permission, put a <uses-permission> element in your app manifest, as a child of the top-level <manifest> element.for example an app that’s need to use CAMERA so we should add its appropriate permissions in the manifest.xml
<?xml version=”1.0" encoding=”utf-8"?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android" package=”com.edu.tce.androidpermissions”>
<uses-permission android:name=”android.permission.CAMERA”/>
<application>
….
<activity>
….
</activity>
</application>
….
</manifest>
Android System Permission Types
📌 Normal Permissions
Normal Permissions which are no great risk to the user’s privacy or the operations of other apps for example your app request the permission to set the timezone is a normal permission.
📌 Dangerous Permissions
Dangerous Permissions which request the resources or a data that involves the user’s private information so it potentially affect the user’s stored data or the operations of other apps for example your app request to read user’s contact is a dangerous permission.
Permission Groups
All the dangerous android system permissions are belong to Permission Groups and following are the behaviors that are happen when you app request the dangerous permissions.
🔑 If your app currently not having any permissions in the permissions groups the system shows a dialog box to the user for approval.for example if an app request the READ_CONTACTS permission the system show a dialog box just say the app needs access to the device’s contact.
🔑 If your app currently having the dangerous permission and now request another permission in the same group the system immediately grants the permission without any interaction with the user.for example if an app had previously requested and been granted the READ_CONTACTS permission, and it then requests WRITE_CONTACTS, the system immediately grants that permission.
The following are the some Dangerous Permissions and their Groups
Get Ramkumar N’s stories in your inbox
Join Medium for free to get updates from this writer.
CALENDAR
READ_CALENDAR
WRITE_CALENDAR
CONTACTS
READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
STORAGE
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE
PHONE
READ_PHONE_STATE
CALL_PHONE
READ_CALL_LOG
WRITE_CALL_LOG
ADD_VOICEMAIL
USE_SIP
PROCESS_OUTGOING_CALLS
Requesting Permissions at Run Time
After Marshmallow(Android 6.0 — API level 23) devices or higher version’s users can grant the permissions while the app is running,not when they install the app.for example now your app needs CAMERA permission the following are the important factors while requesting that permission .
📌 Check For Permission
If your app needs a dangerous permission you must check whether you have that permission every time you perform an operation that requires that permission
ContextCompat.checkSelfPermission() — this method returns PackageManager.PERMISSION_GRANTED if you have this permission else it returns PERMISSION_DENIED.
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED)
{
//to do further actions..
}
📌 Explain why the app needs Permission
If the user chose the Don’t ask again option in the permission request system dialog for a particular permission but after some time the user may want to use functionality that requires a permission but keeps turning down the permission request, that probably shows that the user doesn’t understand why the app needs the permission to provide that functionality. In a situation like that, it’s probably a good idea to show an explanation.
shouldShowRequestPermissionRationale() — this method returns true if the app has requested this permission previously and the user denied the request.
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED)
{
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.CAMERA))
{
//to do further actions..
}
}
📌 Request the Permission
If your app doesn’t already have the permission it needs, the app must call one of the requestPermissions() async method to request the appropriate permissions and this method need an integer request code to identify that permission request.the system calls the app’s callback method with the results, passing the same request code that the app passed to requestPermissions().
requestPermissions() — by call this method the system shows a stanard dialog box to the user.
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA},CAMER_PERMISSION_CODE);
📌 Handle the Permissions request response
When your app request permission , the system shows a dialog box to the user for response.after the user responds the system invokes your app’s onRequestPermissionsResult() method, passing it the user response. Your app has to override that method to find out whether the permission was granted. The callback is passed the same request code you passed to requestPermissions().
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == CAMER_PERMISSION_CODE)
{
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(getApplicationContext(),”CAMERA Permission Granted”,Toast.LENGTH_SHORT).show();
}else {
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.CAMERA))
{
//to do further actions..
}else{
Toast.makeText(getApplicationContext(),”Permission Request Failed”,Toast.LENGTH_SHORT).show();
}
}
The dialog box shown by the system describes the Permission Group your app needs access.it does not list the specific permission.for example if you request READ_CALENDAR permission the system dialog shows your app needs access to the device CALENDAR so the user only needs grant permission once for each permission group.if your app requests any other permission (WRITE_CALENDAR) in that group (that are listed in your app manifest), the system automatically grants them.
Hope you have enjoyed this post,Feel free to comment below for doubts or chat with me in Facebook or drop me e-mail for replies. Share is care.










