Last active
September 27, 2018 21:56
-
-
Save felipebelluco/155c2fcc355b9e0757eefe3a332b92e8 to your computer and use it in GitHub Desktop.
Check for permission in Android with Kotlin
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
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { | |
// Permissão ainda não foi concedida | |
} else { | |
// Permissão já foi concedida, já é possível usufruir da funcionalidade | |
} |
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
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { | |
// Verifica se o usuário já negou acesso a permisão | |
if (shouldShowRequestPermissionRationale(Manifest.permission.CALL_PHONE)) { | |
// Usuário negou acesso | |
// Mostrar uma dialog explicando a importância do app ter acesso a funcionalidade | |
} else { | |
// Usuário não negou acesso ou negou e marcou a caixa de "não perguntar novamente" | |
// Fazer a requisição da permissão | |
} | |
} |
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
AlertDialog.Builder(thisActivity).apply { | |
setMessage(R.string.more_info_dialog_message) | |
setTitle(R.string.more_info_dialog_title) | |
setPositiveButton(R.string.yes_please) { d, i -> | |
// Se o usuário quiser, requere novamente permissão à funcionalidade | |
ActivityCompat.requestPermissions( | |
thisActivity, | |
arrayOf(Manifest.permission.CALL_PHONE), | |
CALL_PHONE_RESULT_CODE) | |
} | |
// Se o usuário optar por não conceder a permissão, fecha a dialog | |
setNegativeButton(R.string.no_thanks) { d, i -> d.dismiss() } | |
}.show() |
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
AlertDialog.Builder(thisActivity).apply { | |
setMessage(R.string.never_ask_again_message) | |
setTitle(R.string.never_ask_again_title) | |
setPositiveButton(R.string.go_to_settings) { d, i -> | |
// Cria intent para a tela de detalhes do app onde é possível o usuário conceder permissão à funcionalidade | |
val appSettings = Intent().apply { | |
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS | |
data = Uri.fromParts("package", thisActivity.packageName, null) | |
} | |
startActivity(appSettings) | |
} | |
setNegativeButton("I've made my mind!") { d, i -> d.dismiss() } | |
}.show() |
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
private val CALL_PHONE_RESULT_CODE = 111 | |
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { | |
when (requestCode) { | |
CALL_PHONE_RESULT_CODE -> { | |
if (grantResults.isNotEmpty() && grantResults.first() == PackageManager.PERMISSION_GRANTED) { | |
// Permissão foi concedida, já é possível usufruir da funcionalidade | |
} else if (!shouldShowRequestPermissionRationale(Manifest.permission.CALL_PHONE)) { | |
// Usuário marcou a caixa "não perguntar novamente" | |
// Mostrar uma dialog explicando a importância do app ter acesso a funcionalidade | |
} else { | |
// Usuário negou acesso à permissão | |
// Bloquear trecho que utilizava a funcionalidade ou informar o usuário da necessidade de ter acesso à funcionalidade | |
} | |
} | |
} | |
} |
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
ActivityCompat.requestPermissions( | |
thisActivity, | |
arrayOf(Manifest.permission.CALL_PHONE), | |
CALL_PHONE_RESULT_CODE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment