Last active
May 24, 2021 19:15
-
-
Save barbeau/ef9291a72b8424ea9c5ec8177dacbb9f to your computer and use it in GitHub Desktop.
Medium article - Room + Flow for location updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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