Skip to content

Instantly share code, notes, and snippets.

@davidgassner
Last active September 30, 2018 09:15
Show Gist options
  • Save davidgassner/91269c304197017be1f4 to your computer and use it in GitHub Desktop.
Save davidgassner/91269c304197017be1f4 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