Friday, November 17, 2017

Android Runtime permission

Welcome to this post.

In this post, I am going to discussing a new topic that is android runtime permission.

What is runtime permission?
Basically, when we want to access some data on the device or want to make a network request from our app, we need user permission.Basically, we need to write the permissions on the AndroidMenifest.xml
AndroidMenifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
if the user installs the app, the user must accept the permissions that we write on AndroidMenifest.xml
But in Android 6.0 API 23
The Android team want to give more control to the user and now user can install without accepting all the permission.
For an example, we want to access to the storage(external storage), so we need permission first. Now we promote to the user to accept the permission in the app runtime to access the storage.
If the user does not accept the permission that means we do not access the storage.

So enough talk, Lets jump into the code-
First, we need to add permission in the manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Create an Int type variable and assign with an integer number
private static final int PERMISSIONS_READ_STORAGE = 0;
For check permission, call checkSelfPermission() method.
if (Build.VERSION.SDK_INT >= 23) {

    if (getContext().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
        PackageManager.PERMISSION_GRANTED) {
           requestPermissions(new String[]
           {Manifest.permission.WRITE_EXTERNAL_STORAGE},
           PERMISSIONS_READ_STORAGE);
               return;
      } 

  } else {
     //pre android 6
}

Note: To support lower API (lower than 23), to support lower API write your code in the else block.

It will promote for user choice and we get result from the onRequestPermissionsResult() method
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], 
                           int[] grantResults) {

    switch (requestCode) {
        case PERMISSIONS_READ_STORAGE: {

            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(MainActivity.this, "Permission Granted", 
                      Toast.LENGTH_LONG).show();

            } else {

                Toast.makeText(MainActivity.this, "Permission denied", 
                      Toast.LENGTH_LONG).show();

                // permission denied, Disable
               
            }
            return;
        }
    }

}

That's it we finished the runtime permission. It's pretty easy.

If you want to take multiple permission at the same time then what?. How you can request multiple permission at the same time?

Multiple permission at the same time
First, add permission at the AndroidMenifest.XML
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
we request for those two and used previously created variable
if (Build.VERSION.SDK_INT >= 23) {


        List<String> listPermissionsNeeded = new ArrayList<>();

        if (checkSelfPermission(Manifest.permission.READ_CONTACTS) != 
             PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS);
        }
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != 
           PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), 
                     PERMISSIONS_READ_CONTACTS);
        }

    } else {
        // Pre-Marshmallow
    }
 Now check on the onRequestPermissionsResult() method
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], 
                           int[] grantResults) {

    switch (requestCode) {
        case PERMISSIONS_READ_STORAGE: {

            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(MainActivity.this, "Permission Granted", 
                      Toast.LENGTH_S
).show();

            } else {

                Toast.makeText(MainActivity.this, "Permission denied", 
                      Toast.LENGTH_SHORT).show();

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
    }

}

That's it
Finished all the code.
Hope this post will help you.

Happy coding.

About Author:

I am Shudipto Trafder,
Since 2015, I write android code. I love to implement new ideas. I use java and also kotlin. Now I am learning Flutter. I love to learn new things and also love to share. And that is the main reason to run this blog.


Let's Get Connected: Facebook Twitter Linkedin Github

No comments :