Skip to content

Instantly share code, notes, and snippets.

@barbeau
Last active May 24, 2021 19:15
Show Gist options
  • Save barbeau/ef9291a72b8424ea9c5ec8177dacbb9f to your computer and use it in GitHub Desktop.
Save barbeau/ef9291a72b8424ea9c5ec8177dacbb9f to your computer and use it in GitHub Desktop.
Medium article - Room + Flow for location updates
@AndroidEntryPoint
class ForegroundOnlyLocationService : LifecycleService() {
...
// Data store (in this case, Room database) where the service will persist the location data, injected via Hilt
@Inject
lateinit var repository: LocationRepository
override fun onCreate() {
...
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
...
// Notify our Activity that a new location was observed by adding to repository
locationResult.lastLocation.toLocation()?.let {
lifecycleScope.launch {
repository.updateLocation(it)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment