Runtime permissions ask users for access only when needed. This helps keep their private data safe.
Why runtime permissions protect user privacy in Android Kotlin
Introduction
Syntax
Android Kotlin
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), REQUEST_CODE)
}Use
checkSelfPermission to see if permission is already granted.Use
requestPermissions to ask the user for permission at runtime.Examples
Android Kotlin
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_REQUEST_CODE)
}Android Kotlin
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS), CONTACTS_REQUEST_CODE)
}Sample App
This app checks if it has camera permission when it starts. If not, it asks the user. It shows a message depending on the user's choice.
Android Kotlin
import android.Manifest import android.content.pm.PackageManager import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import android.widget.Toast class MainActivity : AppCompatActivity() { private val CAMERA_REQUEST_CODE = 100 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(android.R.layout.simple_list_item_1) if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE) } else { Toast.makeText(this, "Camera permission already granted", Toast.LENGTH_SHORT).show() } } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == CAMERA_REQUEST_CODE) { if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { Toast.makeText(this, "Camera permission granted", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show() } } } }
Important Notes
Always explain to users why your app needs a permission before asking.
Users can deny permissions, so your app should handle that gracefully.
Request only the permissions your app really needs to build trust.
Summary
Runtime permissions protect user privacy by asking only when needed.
Check permission status before requesting to avoid unnecessary prompts.
Handle user responses to permissions carefully to keep app working well.
Practice
1. Why do Android apps request runtime permissions instead of asking for all permissions at install time?
easy
Solution
Step 1: Understand runtime permissions purpose
Runtime permissions ask users only when a feature needs access, protecting privacy.Step 2: Compare with install-time permissions
Install-time permissions ask all at once, which can overwhelm users and reduce control.Final Answer:
To protect user privacy by asking only when the permission is needed -> Option AQuick Check:
Runtime permissions = protect privacy [OK]
Hint: Runtime permissions ask only when needed, protecting privacy [OK]
Common Mistakes:
- Thinking runtime permissions speed up installation
- Believing runtime permissions reduce app size
- Assuming runtime permissions avoid user prompts
2. Which Kotlin code snippet correctly checks if the app has permission to access the camera before requesting it?
easy
Solution
Step 1: Identify correct permission check method
ContextCompat.checkSelfPermission returns permission status in Android Kotlin.Step 2: Verify correct comparison
Compare result to PackageManager.PERMISSION_GRANTED to confirm permission granted.Final Answer:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { /* permission granted */ } -> Option CQuick Check:
Use ContextCompat.checkSelfPermission == PERMISSION_GRANTED [OK]
Hint: Use ContextCompat.checkSelfPermission with PERMISSION_GRANTED check [OK]
Common Mistakes:
- Using non-existent methods like checkPermission or hasPermission
- Calling requestPermission instead of checking permission
- Not comparing to PERMISSION_GRANTED constant
3. Given this Kotlin code snippet, what will be the output if the user denies the permission request?
if (ContextCompat.checkSelfPermission(this, Manifest.permission.LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.LOCATION), 100)
} else {
println("Permission already granted")
}medium
Solution
Step 1: Analyze permission check condition
Permission is not granted, so requestPermissions is called to ask user.Step 2: Understand immediate output
requestPermissions shows dialog asynchronously; no immediate print happens.Final Answer:
Nothing printed immediately; permission request dialog shown -> Option AQuick Check:
Permission request dialog shows, no immediate print [OK]
Hint: requestPermissions shows dialog, no immediate output printed [OK]
Common Mistakes:
- Assuming println runs before user responds
- Expecting crash without handling permission denial
- Thinking denial message prints automatically
4. What is wrong with this Kotlin code snippet for requesting permission?
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS), 1)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
println("Permission granted")
}medium
Solution
Step 1: Understand requestPermissions behavior
requestPermissions shows dialog asynchronously; user response is not immediate.Step 2: Identify timing of permission check
Checking permission immediately after request will not reflect user's choice yet.Final Answer:
Permission check is done immediately after request, before user responds -> Option BQuick Check:
Check permission after user response, not immediately [OK]
Hint: Check permission after user responds, not right after request [OK]
Common Mistakes:
- Checking permission immediately after requestPermissions call
- Assuming requestPermissions blocks until user answers
- Misusing permission constants
5. An app needs to access the user's location only when showing nearby stores. How should it use runtime permissions to protect user privacy best?
hard
Solution
Step 1: Identify when location is needed
Location is needed only when user views nearby stores, not before.Step 2: Apply runtime permission best practice
Request permission only at the moment feature is used to respect privacy and reduce prompts.Final Answer:
Request location permission only when the user tries to view nearby stores -> Option DQuick Check:
Request permission when feature used, protect privacy [OK]
Hint: Ask permission only when feature needs it, not before [OK]
Common Mistakes:
- Requesting permission too early at app start
- Requesting all permissions upfront at install
- Avoiding permission request and guessing location
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
