Make one class in with common methods can be added in it. I call it CM as common methods. Add the following code in the common code.
CM.java
public static void showMessageOKCancel(Activity context, String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}public static void applyMarshmallowPermission(
final Activity context, String Permission[], String[] strPermissionMessage,
final int RequestCode, OnPermissionAlreadyGrant callBackPermission) {
final List<String> permissionsList = new ArrayList<String>();
List<String> permissionsNeeded = new ArrayList<String>();
for (int i = 0; i < Permission.length; i++) {
if (!addPermission(context, permissionsList, Permission[i])) {
permissionsNeeded.add(strPermissionMessage[i]);
}
}
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(context, message,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(context, permissionsList.toArray(new String[permissionsList.size()]), RequestCode);
}
});
return;
}
ActivityCompat.requestPermissions(context, permissionsList.toArray(new String[permissionsList.size()]), RequestCode);
return;
}
if (callBackPermission != null) {
callBackPermission.onPermssionAlreadyGranted();
return;
}
}
private static boolean addPermission(Activity context,
final List<String> permissionsList = new ArrayList<String>();
List<String> permissionsNeeded = new ArrayList<String>();
for (int i = 0; i < Permission.length; i++) {
if (!addPermission(context, permissionsList, Permission[i])) {
permissionsNeeded.add(strPermissionMessage[i]);
}
}
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(context, message,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(context, permissionsList.toArray(new String[permissionsList.size()]), RequestCode);
}
});
return;
}
ActivityCompat.requestPermissions(context, permissionsList.toArray(new String[permissionsList.size()]), RequestCode);
return;
}
if (callBackPermission != null) {
callBackPermission.onPermssionAlreadyGranted();
return;
}
}
private static boolean addPermission(Activity context,
List<String> permissionsList, String permission) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (ActivityCompat.shouldShowRequestPermissionRationale(context, permission)) {
return false;
}
}
return true;
}
permissionsList.add(permission);
// Check for Rationale Option
if (ActivityCompat.shouldShowRequestPermissionRationale(context, permission)) {
return false;
}
}
return true;
}
public static int checkNeverAskAgainList(Activity
context, String[] permissionsList) {
int deniedList = 0;
for (String strPermission : permissionsList) {
boolean isRational = ActivityCompat.shouldShowRequestPermissionRationale(context, strPermission);
if (isRational) {
deniedList++;
}
}
return deniedList;
}
public static void goToSettings(Activity context) {
Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + context.getPackageName()));
myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);
myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myAppSettings);
}
context, String[] permissionsList) {
int deniedList = 0;
for (String strPermission : permissionsList) {
boolean isRational = ActivityCompat.shouldShowRequestPermissionRationale(context, strPermission);
if (isRational) {
deniedList++;
}
}
return deniedList;
}
public static void goToSettings(Activity context) {
Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + context.getPackageName()));
myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);
myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myAppSettings);
}
This methods are used to check for permissions that user has to allow or user will go to setting and allow the permission that our app needs.
Now how to use this for multiple permission grant in your activity using this methods.
Add this in your activity when you need to check the permissions
//Mention list of permissions that are needed in your activity
String strPermissoinList[] = new String[]{android.Manifest.permission.READ_PHONE_STATE, android.Manifest.permission.GET_ACCOUNTS};
// Description to be shown to user for particular permission if user denied once.
String strPermissionMessage[] = new String[]{"Phone State", "Google+ Account"};
// Request code to be used when permission is received from user
public static final int PERMISSION_MULTIPLE_GRANT_CODE = 1001;
public void buttonClick(View view) {
CM.applyMarshmallowPermission(MainActivity.this, strPermissoinList, strPermissionMessage, PERMISSION_MULTIPLE_GRANT_CODE,
new OnPermissionAlreadyGrant() {
@Override
public void onPermssionAlreadyGranted() {
// if you have already granted or sdk version is below marshmellow
Toast.makeText(MainActivity.this, "You have allowed all permission. applyMarshmallowPermission : " + CM.getDeviceId(MainActivity.this), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onPermssionAlreadyGranted() {
// if you have already granted or sdk version is below marshmellow
Toast.makeText(MainActivity.this, "You have allowed all permission. applyMarshmallowPermission : " + CM.getDeviceId(MainActivity.this), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case PERMISSION_MULTIPLE_GRANT_CODE:
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(android.Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED);
perms.put(android.Manifest.permission.GET_ACCOUNTS, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for READ_PHONE_STATE
if (perms.get(android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
&& perms.get(android.Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "You have allowed all permission. onRequestPermissionsResult :: " + CM.getDeviceId(MainActivity.this), Toast.LENGTH_SHORT)
.show();
} else {
int countBlock = CM.checkNeverAskAgainList(this, strPermissoinList);
if (countBlock > 0) {
// Permission Denied
Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT)
.show();
} else {
CM.showMessageOKCancel(MainActivity.this, "Go to setting and give permission",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
CM.goToSettings(MainActivity.this);
}
});
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
&& perms.get(android.Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "You have allowed all permission. onRequestPermissionsResult :: " + CM.getDeviceId(MainActivity.this), Toast.LENGTH_SHORT)
.show();
} else {
int countBlock = CM.checkNeverAskAgainList(this, strPermissoinList);
if (countBlock > 0) {
// Permission Denied
Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT)
.show();
} else {
CM.showMessageOKCancel(MainActivity.this, "Go to setting and give permission",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
CM.goToSettings(MainActivity.this);
}
});
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Finally the callback interface that you need to define in your code is below
void onPermssionAlreadyGranted();
}