Forked from davidgassner/RuntimePermissionRequest
Last active
August 21, 2018 07:11
-
-
Save OrenBochman/9f9db65c64f578940033142cd3846bca to your computer and use it in GitHub Desktop.
ask for permission to make a phone call on 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
//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