Last active
December 29, 2020 18:06
-
-
Save magdamiu/c0fd10ca83d19b3b861b0e7fac687d8b to your computer and use it in GitHub Desktop.
Check permissions in Android
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
// 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