0
0
Android Kotlinmobile~20 mins

Location services in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Location Services Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this code when location permission is denied?

Consider this Kotlin snippet for requesting location permission in an Android app. What will the app display if the user denies the location permission?

Android Kotlin
val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
  if (isGranted) {
    textView.text = "Permission granted"
  } else {
    textView.text = "Permission denied"
  }
}

requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
AThe TextView shows "Permission denied"
BThe TextView shows "Permission granted"
CThe app crashes with SecurityException
DThe TextView remains empty
Attempts:
2 left
💡 Hint

Think about what happens when the permission callback receives false.

🧠 Conceptual
intermediate
1:30remaining
Which location provider gives the most accurate location?

Android offers different location providers. Which one typically provides the most accurate location data?

AGPS provider (uses satellites)
BNetwork provider (uses Wi-Fi and cell towers)
CPassive provider (listens to other apps' location requests)
DBluetooth provider
Attempts:
2 left
💡 Hint

Think about which source uses satellites directly.

lifecycle
advanced
2:00remaining
What happens if you start location updates in onCreate and never stop them?

In an Android app, if you start location updates in the onCreate method of an Activity and never stop them, what is the likely result?

ALocation updates only run while Activity is visible
BLocation updates stop automatically when Activity is paused
CApp crashes with IllegalStateException
DLocation updates continue, causing battery drain even when app is in background
Attempts:
2 left
💡 Hint

Consider what happens if you don't remove location updates explicitly.

navigation
advanced
1:30remaining
How to navigate to app settings to enable location permission?

Which Intent action correctly opens the app's settings page so the user can enable location permission manually?

AIntent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
BIntent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { data = Uri.fromParts("package", packageName, null) }
CIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
DIntent(Settings.ACTION_WIFI_SETTINGS)
Attempts:
2 left
💡 Hint

Think about how to open the specific app's settings page.

🔧 Debug
expert
2:30remaining
Why does this location request code cause a crash?

Examine this Kotlin code snippet. Why does it cause a crash when run?

Android Kotlin
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.lastLocation.addOnSuccessListener { location ->
  if (location != null) {
    textView.text = "Lat: ${location.latitude}, Lon: ${location.longitude}"
  } else {
    textView.text = "Location not available"
  }
}
AIt crashes because addOnSuccessListener is not a valid method
BIt crashes because fusedLocationClient is not initialized properly
CIt crashes because location can be null and code does not check for null
DIt crashes because textView is not declared
Attempts:
2 left
💡 Hint

Think about what happens if the location is not available yet.