Skip to content

Instantly share code, notes, and snippets.

@Nilpo
Forked from davidgassner/RuntimePermissionRequest
Created November 12, 2016 23:48
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 Nilpo/2c0f4d2cff4e239afdbec8313f6c4b88 to your computer and use it in GitHub Desktop.
Save Nilpo/2c0f4d2cff4e239afdbec8313f6c4b88 to your computer and use it in GitHub Desktop.
//Add before making a phone call
if (ActivityCompat.checkSelfPermission(
MainActivity.this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.CALL_PHONE)) {
Toast.makeText(MainActivity.this, "I know you said no, but I'm asking again", Toast.LENGTH_SHORT).show();
}
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
return;
}
//End of addition
//Add this method to handle the result of the permission request
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Permission was granted!",
Toast.LENGTH_SHORT).show();
callForInfo();
} else {
Toast.makeText(MainActivity.this, "Permission was denied!",
Toast.LENGTH_SHORT).show();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment