Skip to content

Instantly share code, notes, and snippets.

@eefret
Last active March 17, 2016 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eefret/49e713f0b9d54c5d3678 to your computer and use it in GitHub Desktop.
Save eefret/49e713f0b9d54c5d3678 to your computer and use it in GitHub Desktop.
This are some methods I usually use to handle android 6 permissions
/**
* Check whether the passed permissions are granted and if not it requests them.
* @param activity The activity that requires the permissions
* @param requestCode The request code
* @param permissions The permissions you want to grant
*/
public static void requestPermissionsNotAllowed(@NonNull Activity activity,
@NonNull int requestCode,
@NonNull String... permissions){
ArrayList<String> missing = new ArrayList<>(permissions.length);
for(String perm : permissions){
int resp = ContextCompat.checkSelfPermission(activity,perm);
if(resp != PackageManager.PERMISSION_GRANTED){
missing.add(perm);
}
}
ActivityCompat.requestPermissions(activity,
missing.toArray(new String[missing.size()]),
requestCode);
}
/**
* Returns True if the permission is granted false otherwise
* @param context The context from where its being asked
* @param permission The permission to be asked @see android.Manifest.permission
* @return true if granted, else false
*/
public static boolean isPermissionGranted(@NonNull Context context, @NonNull String permission){
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment