Asking for multiple permissions in android Java

You are currently viewing Asking for multiple permissions in android Java
Request Multiple Permission In Android
  • Post author:
  • Post category:Blog
  • Post comments:0 Comments

Hello dear coders welcome back, today we will learn how to check for multiple permission in android application. When we install any android app that application may ask for different permission example :

  • location permission
  • camera permission
  • storage permission
  • internet permission etc.

If you want to know how this android application ask for multiple permission when we install app for first time then you are on the perfect place to solve your doubt.

Here we are going to create a app which will show prompt and ask to allow CAMERE and STORAGE permission.

Adding permission in manifest file

				
					<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <application
        android:allowBackup="true"
        android:requestLegacyExternalStorage="true"
      >
    </application>
</manifest>
				
			

Creating unique code for each permission

				
					private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static final int MY_CAMERA_REQUEST_CODE = 2;
				
			

Reading permission from Manifest

				
					private static String[] PERMISSION_STORAGE={
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };

    private static String[] PERMISSION_CAMERA={
            Manifest.permission.CAMERA
    };
				
			

Using ActivityCompat.checkSelfPermission

To check permission we have to use ActivityCompat.checkSelfPermission and PackageManager.PERMISSION_GRANTED  together to verify permission in code.

				
					public static void verifyPermissions(Activity activity) {
        int storagePermission = ActivityCompat.checkSelfPermission(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int cameraPermission = ActivityCompat.checkSelfPermission(activity,Manifest.permission.CAMERA);

        if(storagePermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity,
                    PERMISSION_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
                    );
        }

        if(cameraPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity,
                    PERMISSION_CAMERA,
                    MY_CAMERA_REQUEST_CODE
            );
        }
    }
				
			

Call verifyPermissions method

Finally we have to call verifyPermissions method to get all required permission. You can call this method at any point of time when required, In our case we are calling this method into onCreate method.

				
					 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        verifyPermissions(this);
    }
				
			

Note : If user decline for permission then whenever permission is required then we need to call verifyPermissions  method again.

Leave a Reply