Android multiple runtime permission

Android multiple runtime permission

Step -1 : Add the permissions on Menifest.

 <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>

Step 2 : Request for permissions

//Permission function starts from here
    private void RequestMultiplePermission() {

        // Creating String Array with Permissions.
        ActivityCompat.requestPermissions(MainActivity.this, new String[]
                {
                        READ_EXTERNAL_STORAGE,
                        RECORD_AUDIO,
                        WRITE_EXTERNAL_STORAGE,

                }, RequestPermissionCode);

    }

    // Calling override method.
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {

            case RequestPermissionCode:

                if (grantResults.length > 0) {

                    boolean readExternalStorage = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                    boolean recordAudio = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                    boolean writeExternalStorage = grantResults[2] == PackageManager.PERMISSION_GRANTED;

                    if (readExternalStorage && recordAudio && writeExternalStorage ) {

                        Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_LONG).show();
                    }
                    else {
                        Toast.makeText(MainActivity.this,"Permission Denied",Toast.LENGTH_LONG).show();

                    }
                }

                break;
        }
    }

Step 3: Check permissions

  // Checking permission is enabled or not using function starts from here.
    public boolean CheckingPermissionIsEnabledOrNot() {

        int recordAudio = ContextCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO);
        int readExternalStorage = ContextCompat.checkSelfPermission(getApplicationContext(), READ_EXTERNAL_STORAGE);
        int writeExternalStorage = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);

        return recordAudio == PackageManager.PERMISSION_GRANTED &&
                readExternalStorage == PackageManager.PERMISSION_GRANTED &&
                writeExternalStorage == PackageManager.PERMISSION_GRANTED;
    }

Step 4 : Call it on OnCreate to check permissions are granted or not.

   if(CheckingPermissionIsEnabledOrNot())
        {
            Toast.makeText(MainActivity.this, "All Permissions Granted Successfully", Toast.LENGTH_LONG).show();
        }

        // If, If permission is not enabled then else condition will execute.
        else {

            //Calling method to enable permission.
            RequestMultiplePermission();

        }

Full Code

 

import android.Manifest;

import android.app.Activity;

import android.content.Context;

import android.content.DialogInterface;

import android.content.pm.PackageManager;

import android.os.Build;

import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v4.app.ActivityCompat;

import android.support.v4.content.ContextCompat;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.view.View;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Button;

import android.widget.Toast;



import android.app.AlertDialog;

public class MainActivity extends AppCompatActivity {

private static final int MY_PERMISSIONS_REQUEST_CODE = 123;



private Context mContext;

private Activity mActivity;

private Button permissionCheck;



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);



// Get the application context

mContext = getApplicationContext();

mActivity = MainActivity.this;



permissionCheck = findViewById(R.id.permission_check);

permissionCheck.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){

checkPermission();

}

}

});
}

protected void checkPermission(){

if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CAMERA)

+ ContextCompat.checkSelfPermission(

mActivity,Manifest.permission.READ_CONTACTS)

+ ContextCompat.checkSelfPermission(

mActivity,Manifest.permission.WRITE_EXTERNAL_STORAGE)

!= PackageManager.PERMISSION_GRANTED){



// Do something, when permissions not granted

if(ActivityCompat.shouldShowRequestPermissionRationale(

mActivity,Manifest.permission.CAMERA)

|| ActivityCompat.shouldShowRequestPermissionRationale(

mActivity,Manifest.permission.READ_CONTACTS)

|| ActivityCompat.shouldShowRequestPermissionRationale(

mActivity,Manifest.permission.WRITE_EXTERNAL_STORAGE)){

// If we should give explanation of requested permissions



// Show an alert dialog here with request explanation

AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

builder.setMessage("Camera, Read Contacts and Write External" +

" Storage permissions are required to do the task.");

builder.setTitle("Please grant those permissions");

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialogInterface, int i) {

ActivityCompat.requestPermissions(

mActivity,

new String[]{

Manifest.permission.CAMERA,

Manifest.permission.READ_CONTACTS,

Manifest.permission.WRITE_EXTERNAL_STORAGE

},

MY_PERMISSIONS_REQUEST_CODE

);

}

});

builder.setNeutralButton("Cancel",null);

AlertDialog dialog = builder.create();

dialog.show();

}else{

// Directly request for required permissions, without explanation

ActivityCompat.requestPermissions(

mActivity,

new String[]{

Manifest.permission.CAMERA,

Manifest.permission.READ_CONTACTS,

Manifest.permission.WRITE_EXTERNAL_STORAGE

},

MY_PERMISSIONS_REQUEST_CODE

);

}

}else {

// Do something, when permissions are already granted

Toast.makeText(mContext,"Permissions already granted",Toast.LENGTH_SHORT).show();

}

}



@Override

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){

switch (requestCode){

case MY_PERMISSIONS_REQUEST_CODE:{

// When request is cancelled, the results array are empty

if(

(grantResults.length >0) &&

(grantResults[0]

+ grantResults[1]

+ grantResults[2]

== PackageManager.PERMISSION_GRANTED

)

){

// Permissions are granted

Toast.makeText(mContext,"Permissions granted.",Toast.LENGTH_SHORT).show();

}else {

// Permissions are denied

Toast.makeText(mContext,"Permissions denied.",Toast.LENGTH_SHORT).show();

}

return;

}

}

}

}

Download From Github

https://github.com/arifhasnatnstucsteonGit/Android_Multiple_Runtime_Permission

Leave a Reply

Your email address will not be published. Required fields are marked *