Skip to content

Instantly share code, notes, and snippets.

@oluwabajio
Last active April 26, 2021 12:10
Show Gist options
  • Save oluwabajio/8c6c712c296cba63250a05339d912179 to your computer and use it in GitHub Desktop.
Save oluwabajio/8c6c712c296cba63250a05339d912179 to your computer and use it in GitHub Desktop.
Android Permission Helper Class
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class PermissionHelper {
private final static int PERMISSIONCODE = 100;
public static boolean checkPermissions(Context context){
boolean result = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED;
return result;
}
public static void requestPermissions(Activity activity){
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONCODE);
}
public static boolean checkRequest(int requestCode,int[] grantResults){
boolean result = false;
result = requestCode == PERMISSIONCODE
&& grantResults.length == 3
&& grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED
&& grantResults[2] == PackageManager.PERMISSION_GRANTED;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment