android - Request multiple permissions example






activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
android:background="#e5ead3"
>
<Button
android:id="@+id/btn_do_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Permissions"
/>
</LinearLayout>



MainActivity.java



package com.cfsuman.me.androidcodesnippets;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
private Context mContext;
private Activity mActivity;

private LinearLayout mRootLayout;
private Button mBtnDoTask;

private static final int MY_PERMISSIONS_REQUEST_CODE = 123;

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

// Get the application context
mContext = getApplicationContext();
mActivity = MainActivity.this;

// Get the widget reference from xml layout
mRootLayout = findViewById(R.id.root_layout);
mBtnDoTask = findViewById(R.id.btn_do_task);

// Set a click listener for the button
mBtnDoTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
checkPermission();
}
}
});
}

protected void checkPermission(){
if(ContextCompat.checkSelfPermission(mActivity,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;
}
}
}
}



AndroidManifest.xml [Permissions]



<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>