Skip to content

Instantly share code, notes, and snippets.

@PatilSiddhesh
Last active February 17, 2020 03:16
Show Gist options
  • Save PatilSiddhesh/a3e3fa8d9fb77939d15879aff2a36b76 to your computer and use it in GitHub Desktop.
Save PatilSiddhesh/a3e3fa8d9fb77939d15879aff2a36b76 to your computer and use it in GitHub Desktop.
/**
* 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