Last active
February 17, 2020 03:16
-
-
Save PatilSiddhesh/a3e3fa8d9fb77939d15879aff2a36b76 to your computer and use it in GitHub Desktop.
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
/** | |
* Checks permission, if permission has been denied shows rationale for the permission | |
*/ | |
private fun checkContactsPermission() { | |
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS) | |
!= PackageManager.PERMISSION_GRANTED | |
) { | |
// Check if permission is not granted | |
Log.d(TAG, "Permission for contacts is not granted") | |
// This condition only becomes true if the user has denied the permission previously | |
if (shouldShowRequestPermissionRationale(android.Manifest.permission.READ_CONTACTS)) { | |
showRationaleDialog( | |
getString(R.string.rationale_title), | |
getString(R.string.rationale_desc), | |
android.Manifest.permission.READ_CONTACTS, | |
REQUEST_CONTACTS_STATE | |
) | |
} else { | |
// Perform a permission check | |
Log.d(TAG, "Checking permission") | |
requestPermissions( | |
arrayOf(android.Manifest.permission.READ_CONTACTS), | |
REQUEST_CONTACTS_STATE | |
) | |
} | |
} else { | |
// Permission is already granted, do your magic here! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment