Search code examples
javaandroidbackground-service

Background Service for update location in background


it kills background service, to solve your issue you should use foreground service.

my background sticky service is kill on oreo and heigher devices any solution for getting location on background service when Activity is on backound


Solution

  • It's because of Android Oreo behaviour changes for background execution. Suggested alternatives are

    1) Foreground service.

    Use this if you are ok displaying notification to the user when you are trying to retrieve the location using Location API. This would be reliable and is suggested in the documentation.

    Sample app from the documentation : LocationUpdatesForegroundService project on GitHub

    An example of an app that allows the app to continue a user-initiated action without requesting all-the-time access to background location.

    References
    https://developer.android.com/training/location/receive-location-updates
    

    2) Work Manager

    This approach would be less reliable as you will not be able to control when exactly this would be called but can be used if you don't want to show notification to the user at all.