Skip to content

Instantly share code, notes, and snippets.

@florinmuscalu
Last active February 19, 2021 06:59
Show Gist options
  • Save florinmuscalu/1c674e0004a7e177c6d63a80ad7d31ee to your computer and use it in GitHub Desktop.
Save florinmuscalu/1c674e0004a7e177c6d63a80ad7d31ee to your computer and use it in GitHub Desktop.
private boolean permissions_ok = false;
private static int LOCATION_PERMISSION_CODE = 100;
public void requestPermission(View view) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions_ok = false;
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_CODE);
}
else {
permissions_ok = true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == LOCATION_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
permissions_ok = true;
}
else {
permissions_ok = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment