Background service - Get GPS Location every x minutes?

Hi all,

I'm developing an app using Xamarin (duh), and I need a background service that gets the user's GPS location at recurring intervals (say, every 15 minutes).

This project ( is the closest I can find to example code, and it claims that the location continues to update, even with the app in the background. Out of the box, this doesn't seem to be the case.

Could anyone offer insight (or even better, example code/projects) to point me in the right direction?

Thanks in advance.

Best Answer


  • lakhanimilan91lakhanimilan91 INMember ✭✭

    Hi @Rhonage

    In your above scenario there might be two cases.
    1, Whether application is running in background
    2, Or Location is not getting updated

    1. For first you can use other services facilities provided by Android in which you can keep application awake even in background.
    2. If you didnt get location update then try to use google play service location provider.

    Hope this may help you.


  • RhonageRhonage NZMember ✭✭

    Hi @lakhanimilan91, thanks for your response.

    Re: Here's the log once the application moves into the background, and then it stops updating GPS. I'd just like to know if this is normal behaviour or not, as the project description says otherwise.

    12-12 08:19:12.324 D/MainActivity(10254): Foreground updating
    12-12 08:19:12.345 D/LocationService(10254): Latitude is -44.40749468
    12-12 08:19:12.345 D/LocationService(10254): Longitude is 171.25077323
    12-12 08:19:12.345 D/LocationService(10254): Altitude is 4
    12-12 08:19:12.346 D/LocationService(10254): Speed is 2.37
    12-12 08:19:12.346 D/LocationService(10254): Accuracy is 39.664
    12-12 08:19:12.346 D/LocationService(10254): Bearing is 350.9
    12-12 08:19:12.348 D/MainActivity(10254): Location status changed, event raised
    12-12 08:19:12.917 D/AppTracker(10254): App Event: stop
    12-12 08:19:12.931 D/MainActivity(10254): OnPause: Location app is moving to background

    As soon as "App Event: stop" happens, the app moves into the background and the GPS Service no longer functions. Also, I use Android 8.0 - not sure if that has anything to do with it or not.

    For first you can use other services facilities provided by Android in which you can keep application awake even in background.

    What other services are those?

    If you didnt get location update then try to use google play service location provider.

    Is that Google Fused Provider (LocationClient)?

    Thanks again.

  • RhonageRhonage NZMember ✭✭

    Thanks @RyanFrench - I actually whipped up a Foreground Service last night (I didn't know of them before). It works really well now.

    One thing though, is I'd like to report back to the Main Activity whenever the location is changed (so I can update fields). Usually I'd hook up an event, which is fired from the Location Service - but I don't think I can keep an instance of my service in my Main Activity.

    Would I use a Broadcast Receiver to do this?

  • RyanFrenchRyanFrench USMember ✭✭

    This is what I recommend you do for this situation.

    Have location logger send the locations back to the foreground service either using an event or messaging center (safe method). Next have your service save the locations to a sqlite DB along with any other info from the service that you need.

    Update your main activity to load the values from the db when it loads. You can also set it to refresh on an interval if this will be a long running activity.

  • RhonageRhonage NZMember ✭✭

    Thanks Ryan.

    It will be long running (until the user cancels it). Would running the service and using this suffice? (specifying the minutes)

    this._locationManager.RequestLocationUpdates(provider, [every x minutes], 0.0f, this);

    Also, I have it working well now with a broadcast receiver. Seems to do the trick, but thank you for your reply.

  • RhonageRhonage NZMember ✭✭

    Also I tried using MessagingCenter, but it doesn't look it's available for Xamarin.Native/Android. Seems to be a Xamarin.Forms thing.

  • RyanFrenchRyanFrench USMember ✭✭

    Int64.TryParse(TimeSpan.FromMinutes(LogInterval).TotalMilliseconds.ToString(), out var interval);
    _locMngr.RequestLocationUpdates(interval, 0, _locCriteria, this, _handlerThread.Looper);

    interval will be in miliseconds I believe.

  • RhonageRhonage NZMember ✭✭

    Yes that's correct. I'm more asking if it's a good idea (or not) to simply leave the location manager running to update every (say) 15 minutes for perhaps 12 hours straight - or if it's better to set up an AlarmManager (or similar) and call it that way.

    I haven't delved into mobile dev in years, so I'm uncertain what best practices are :).

  • RyanFrenchRyanFrench USMember ✭✭

    It will be fine to leave that running.
    If really want to get location updates triggered by AlarmManager use the method RequestSingleLocationUpdate() in the class I sent you.

  • RhonageRhonage NZMember ✭✭

    Thanks for all your help pointing me in the right direction Ryan, you've been fantastic.

  • CosysproCosyspro Member ✭✭

    Thanks for your help. Can send me the code of foreground service in which the call of LocationLogger is inserted.

  • SnehalJadhav.0403SnehalJadhav.0403 USMember ✭✭
    edited November 2018

    @RyanFrench , Can you please post the code for foreground service with LocationLogger ?
    @Cosyspro , Did you get the solution?

  • srushteesrushtee Member

    @Rhonage can you please provide me with your foreground service code for location update.. it will be a great help.. thanks

Sign In or Register to comment.