Permissions & Runtime Requests
Declare and request dangerous permissions at runtime using ActivityResultContracts, show rationale dialogs, and handle permanent denial.
Permissions & Runtime Requests is a free Android Academy lesson on CoddyKit — lesson 7 of 7. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 7 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Android Permissions Overview
Android apps must declare which device features they need. Permissions protect user privacy:
- Normal permissions — low risk, granted automatically (INTERNET, VIBRATE)
- Dangerous permissions — access sensitive data, must be requested at runtime (CAMERA, LOCATION, READ_CONTACTS)
- Signature permissions — only for apps signed with the same certificate
Declaring in Manifest
All permissions must first be declared in AndroidManifest.xml:
<!-- AndroidManifest.xml -->
<manifest ...>
<!-- Normal: granted automatically -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<!-- Dangerous: must request at runtime -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application ...>
...
</application>
</manifest>Checking If Permission Is Granted
Before using a dangerous feature, always check if the permission is already granted:
import androidx.core.content.ContextCompat
import android.content.pm.PackageManager
fun isCameraPermissionGranted(): Boolean {
return ContextCompat.checkSelfPermission(
this, // Context
android.Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED
}Requesting Permission with ActivityResultLauncher
Use ActivityResultContracts.RequestPermission() — the modern way (no deprecated onRequestPermissionsResult):
class CameraActivity : AppCompatActivity() {
private val requestCameraPermission = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
openCamera()
} else {
showPermissionDeniedMessage()
}
}
fun onTakePhotoClick(view: View) {
if (isCameraPermissionGranted()) {
openCamera()
} else {
requestCameraPermission.launch(android.Manifest.permission.CAMERA)
}
}
}Multiple Permissions at Once
Request multiple permissions with RequestMultiplePermissions:
private val requestMultiple = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
val cameraGranted = permissions[android.Manifest.permission.CAMERA] ?: false
val locationGranted = permissions[android.Manifest.permission.ACCESS_FINE_LOCATION] ?: false
if (cameraGranted && locationGranted) {
startFeature()
} else {
showWhichPermissionsDenied(cameraGranted, locationGranted)
}
}
fun requestPermissions() {
requestMultiple.launch(arrayOf(
android.Manifest.permission.CAMERA,
android.Manifest.permission.ACCESS_FINE_LOCATION
))
}Rationale: When to Explain Why
shouldShowRequestPermissionRationale() returns true when the user previously denied the permission. Show an explanation before requesting again:
fun requestCameraWithRationale() {
when {
isCameraPermissionGranted() -> {
openCamera()
}
shouldShowRequestPermissionRationale(android.Manifest.permission.CAMERA) -> {
// Explain why we need it, then request
AlertDialog.Builder(this)
.setTitle("Camera needed")
.setMessage("We use the camera to take profile photos.")
.setPositiveButton("OK") { _, _ ->
requestCameraPermission.launch(android.Manifest.permission.CAMERA)
}
.show()
}
else -> {
requestCameraPermission.launch(android.Manifest.permission.CAMERA)
}
}
}Permanently Denied — Send to Settings
If the user taps "Don't ask again", shouldShowRequestPermissionRationale returns false and you can't request again. Direct them to app settings:
fun openAppSettings() {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.fromParts("package", packageName, null)
}
startActivity(intent)
}Location Permission
Location has two levels — choose the right one for your use case:
ACCESS_COARSE_LOCATION— city-level accuracy (WiFi/cell towers)ACCESS_FINE_LOCATION— GPS accuracyACCESS_BACKGROUND_LOCATION— required separately on Android 10+ to access location in background
Camera Permission Full Example
Complete flow combining check → rationale → request:
class PhotoActivity : AppCompatActivity() {
private val cameraLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { granted ->
if (granted) openCamera() else showDenied()
}
private fun handleCameraClick() {
val permission = android.Manifest.permission.CAMERA
when {
ContextCompat.checkSelfPermission(this, permission) ==
PackageManager.PERMISSION_GRANTED -> openCamera()
shouldShowRequestPermissionRationale(permission) ->
showRationaleDialog()
else -> cameraLauncher.launch(permission)
}
}
private fun showDenied() {
Snackbar.make(binding.root, "Camera permission required", Snackbar.LENGTH_LONG)
.setAction("Settings") { openAppSettings() }.show()
}
}Permission Best Practices
Key rules for a smooth permission experience:
- Request permissions only when needed (not at app launch)
- Request only the minimum permissions required
- Always handle the denied case gracefully
- Don't repeatedly ask after "Don't ask again"
- Explain why before the system dialog, not after denial
Android 13+ Fine-Grained Media Permissions
Since Android 13 (API 33), READ_EXTERNAL_STORAGE is split into granular permissions:
READ_MEDIA_IMAGESREAD_MEDIA_VIDEOREAD_MEDIA_AUDIO
Always check Build.VERSION.SDK_INT and request the right permission for the OS version.
Quick Check
When should you show a rationale dialog before requesting a dangerous permission?
Recap: Permissions
Runtime permissions done right:
- Declare in
AndroidManifest.xml - Check with
ContextCompat.checkSelfPermission() - Request via
registerForActivityResult(RequestPermission()) - Show rationale when
shouldShowRequestPermissionRationale()is true - For permanent denial, guide users to Settings
- Never request permissions before the user needs them
Next: save data locally with DataStore.
Frequently asked questions
Is the “Permissions & Runtime Requests” lesson free?
Yes — the full text of “Permissions & Runtime Requests” is free to read here on the web, and the Android Academy course includes 7 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “Permissions & Runtime Requests”?
Declare and request dangerous permissions at runtime using ActivityResultContracts, show rationale dialogs, and handle permanent denial. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 7 of 7, so you can start here or from the beginning and move at your own pace.
How long does the “Permissions & Runtime Requests” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Project Structure & Manifest
- Activities & Lifecycle
- Layouts & Views
- Handling User Input
- Intents & Navigation
- Fragments
- Permissions & Runtime Requests