Last active
February 25, 2021 04:35
-
-
Save zeero0/fd314a281d007283fc7931f1c5da634c to your computer and use it in GitHub Desktop.
Check and Request multiple permissions easily on Android 6.0 (API level 23) and above
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zeeroapps.easy_permissions; | |
import android.Manifest; | |
import android.os.Build; | |
import android.support.annotation.NonNull; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity implements PermissionUtil.PermissionsCallBack{ | |
private static final String TAG = "MainActivity"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
public void requestPermissions(View view) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
if (PermissionUtil.checkAndRequestPermissions(this, | |
Manifest.permission.ACCESS_FINE_LOCATION, | |
Manifest.permission.SEND_SMS)) { | |
Log.i(TAG, "Permissions are granted. Good to go!"); | |
} | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
PermissionUtil.onRequestPermissionsResult(this, requestCode, permissions, grantResults, this); | |
} | |
@Override | |
public void permissionsGranted() { | |
Toast.makeText(this, "Permissions granted!", Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void permissionsDenied() { | |
Toast.makeText(this, "Permissions Denied!", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zeeroapps.easy_permissions; | |
import android.annotation.TargetApi; | |
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.annotation.NonNull; | |
import android.support.annotation.RequiresApi; | |
import android.support.v4.app.ActivityCompat; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class PermissionUtil { | |
public static final int REQUEST_CODE_PERMISSIONS = 100; | |
/** | |
* Check if multiple permissions are granted, if not request them. | |
* | |
* @param activity calling activity which needs permissions. | |
* @param permissions one or more permissions, such as {@link android.Manifest.permission#CAMERA}. | |
* @return true if all permissions are granted, false if at least one is not granted yet. | |
*/ | |
@RequiresApi(api = Build.VERSION_CODES.M) | |
public static boolean checkAndRequestPermissions(Activity activity, String... permissions) { | |
List<String> permissionsList = new ArrayList<>(); | |
for (String permission : permissions) { | |
int permissionState = activity.checkSelfPermission(permission); | |
if (permissionState == PackageManager.PERMISSION_DENIED) { | |
permissionsList.add(permission); | |
} | |
} | |
if (!permissionsList.isEmpty()) { | |
ActivityCompat.requestPermissions(activity, | |
permissionsList.toArray(new String[permissionsList.size()]), | |
REQUEST_CODE_PERMISSIONS); | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Handle the result of permission request, should be called from the calling {@link Activity}'s | |
* {@link ActivityCompat.OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[])} | |
* | |
* @param activity calling activity which needs permissions. | |
* @param requestCode code used for requesting permission. | |
* @param permissions permissions which were requested. | |
* @param grantResults results of request. | |
* @param callBack Callback interface to receive the result of permission request. | |
*/ | |
public static void onRequestPermissionsResult(final Activity activity, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults, final PermissionsCallBack callBack) { | |
if (requestCode == PermissionUtil.REQUEST_CODE_PERMISSIONS && grantResults.length > 0) { | |
final List<String> permissionsList = new ArrayList<>(); | |
for (int i = 0; i < permissions.length; i++) { | |
if (grantResults[i] == PackageManager.PERMISSION_DENIED) { | |
permissionsList.add(permissions[i]); | |
} | |
} | |
if (permissionsList.isEmpty() && callBack != null) { | |
callBack.permissionsGranted(); | |
} else { | |
boolean showRationale = false; | |
for (String permission : permissionsList) { | |
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) { | |
showRationale = true; | |
break; | |
} | |
} | |
if (showRationale) { | |
showAlertDialog(activity, new DialogInterface.OnClickListener() { | |
@TargetApi(Build.VERSION_CODES.M) | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
checkAndRequestPermissions(activity, permissionsList.toArray(new String[permissionsList.size()])); | |
} | |
}, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
if (callBack != null) { | |
callBack.permissionsDenied(); | |
} | |
} | |
}); | |
} | |
} | |
} | |
} | |
/** | |
* Show alert if any permission is denied and ask again for it. | |
* | |
* @param context | |
* @param okListener | |
* @param cancelListener | |
*/ | |
private static void showAlertDialog(Context context, | |
DialogInterface.OnClickListener okListener, | |
DialogInterface.OnClickListener cancelListener) { | |
new AlertDialog.Builder(context) | |
.setMessage("Some permissions are not granted. Application may not work as expected. Do you want to grant them?") | |
.setPositiveButton("OK", okListener) | |
.setNegativeButton("Cancel", cancelListener) | |
.create() | |
.show(); | |
} | |
interface PermissionsCallBack { | |
void permissionsGranted(); | |
void permissionsDenied(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment