Skip to content

Instantly share code, notes, and snippets.

@magdamiu
Last active December 29, 2020 18:06
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 magdamiu/c0fd10ca83d19b3b861b0e7fac687d8b to your computer and use it in GitHub Desktop.
Save magdamiu/c0fd10ca83d19b3b861b0e7fac687d8b to your computer and use it in GitHub Desktop.
Check permissions in Android
// manifest add => <uses-permission android:name="android.permission.CAMERA" />
import static android.Manifest.permission.CAMERA;
private static final int REQUEST_CODE_CAMERA = 23;
if (ContextCompat.checkSelfPermission(StylesActivity.this, CAMERA) != PackageManager.PERMISSION_GRANTED) {
// unhappy path
ActivityCompat.requestPermissions(StylesActivity.this, new String[]{CAMERA}, REQUEST_CODE_CAMERA);
} else {
// happy path
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_CAMERA) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// user accepted to grant camera permission
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment