09. Location

Location sources:

Android has three methods to obtain location:

Permissions:

<manifest>
  // Foreground: one-off access to location
  <uses-permission
    android:name="android.permission.ACCESS_COARSE_LOCATION"
  />
  // If you want fine access, you must also request coarse
  // Gives you accses to GPS location
  <uses-permission
    android:name="android.permission.ACCESS_FINE_LOCATION"
  />
  // Background: requires permission on API level 29 and higher
  <uses-permission
    android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
  />
</manifest>

Older approach was easy to get wrong: forgetting to disable it and leading to large battery drain:

Newer approach uses Google Play Services and provides a higher-level API: