Last active
March 17, 2016 20:39
-
-
Save eefret/49e713f0b9d54c5d3678 to your computer and use it in GitHub Desktop.
This are some methods I usually use to handle android 6 permissions
This file contains hidden or 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
/** | |
* 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