Our Story

Enterprise solutions to empower people.
Company was established in the year 2012. Provide high quality cost effective services to the community and businesses. Goldenmace is a company with young, energetic, curious and committed minds who are excited about solving complex business problems, ethically. We build long term relationships and provide quality products and services that will exceed the expectations of all our customers. We deliver secure, reliable and scalable applications that help businesses excel in today’s rapidly evolving economy.

Working Hours

Monday to Saturday .................... 09:00-18:00
Sunday ......................... CLOSED

info@goldenmace.com

IND +91 22 4004 9616 | USA +1 309 247 5034

Top
 

How to add runtime permission in android?

How to add runtime permission in android?

I know, it is a bit late to discuss about Runtime Permissions as there are two higher version of the Android OS versions already rolled out. Here I want to share the thought how we are integrating Runtime Permissions in our all Android apps. Lets we guide you how to add runtime permission in android.

From Android OS 6.0 (API level 23), user needs to grant some of permission when app is running and app wants to access some of user’s privacy. Below API level 23 a flow remains same as what we were doing previously – no runtime permission requires.

In Android OS 6.0 system permissions are categorized into two categories Normal and Dangerous.Normal declare them in the manifest while Dangerous require runtime without granted dangerous permission call some function app will suddenly throw an Exception which will lead to the application crashing. Hence we need to implement this new runtime permission model in our application.

This blog will help you to the how to implement runtime permission using few line of code. There are many user-permissions in Android but I am only going to focus on some of the most used.

Also demonstrates single permission and group of permission.

 

Check Self Permissions

checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)

 

Requesting Permissions

The method requestPermissions(String[] permissions, int requestCode); is a public method that is used to request dangerous permissions. We can ask for multiple dangerous permissions by passing a string array of permissions.

ActivityCompat.requestPermissions(this, permissions, requestCode);

 

Dangerous vs. Non-Dangerous Permissions

Android defines some permission as “dangerous” and some permission as “normal.” Both are required in your application’s manifest but only dangerous permissions require a runtime request. Normal permissions are accepted at install time and cannot be revoked later. An example of a normal permission is android.permission.INTERNET. Dangerous permissions are grouped into categories that make it easier for the user to understand what they are allowing the application to do. If the user accepts one permission in a group they accept the entire group. The opposite is true as well, if the user denied one permission in a group, the entire group in denied. The example application demonstrates this with both FINE_LOCATION and COARSE_LOCATION of the group LOCATION. You will notice that once the user has granted permission for one, the application is automatically granted permission for the other without the need to ask.

Know more about dangerous permissions from here

 

Code

AndroidManifest.xml

<uses-permission android:name=”android.permission.READ_CONTACTS” />

    <uses-permission android:name=”android.permission.CAMERA” />

    <uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />

 

PermissionSampleActivity.java

To start using this sample project you just need to extends RuntimePermissionActivity :

public class PermissionSampleActivity extends RuntimePermissionActivity {

   …

 }

Single permission

For each single permission :

 

if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {

    askRequestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, R.string.enable_permission, REQUEST_CONTACT_PERMISSIONS);

} else {

    // Permission already granted.

    // add your logic

}

Multiple permissions

For each group permission :

 

if ((ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)

     || (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)

     || (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {

     askRequestPermissions(new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},

             R.string.enable_permission, REQUEST_GROUP_PERMISSIONS);

} else {

      // Permission already granted.

      // add your logic

}

Permission Granted

After granting permission write down your method or custom code :

 

@Override

    public void onRequestPermissionsGranted(int requestCode) {

        switch (requestCode) {

            // Single permission granted

            case REQUEST_CONTACT_PERMISSIONS:

                //TODO Add your logic

                break;

           

            // Group permission granted

            case REQUEST_GROUP_PERMISSIONS:

                //TODO Add your logic

                break;

        }

    }

 

GitHub link : https://github.com/Goldenmace/droid-RuntimePermissions

 

Conclusion

In this blog, you see how to ask runtime permissions, and handle the results with android new permission model. The aim of this new model is to put users in control of what our apps need to do.

No Comments

Leave a Comment