Developing a Location Background Service

mivewimivewi DKMember ✭✭

Hi

I want my app to send a location every x minuttes, both when using the app, but also when the app is in the background.

I seen a few example of how to create a service which is running in the background with xamarin.android. (https://github.com/xamarin/mobile-samples/tree/master/BackgroundLocationDemo)

But what about xamarin.forms, which I'm working on? Could the same code be used with forms? Also I haven't seen the same for xamarin.ios.

Why can't I find a full example code of sending locations from a app, both foreground and background? I'm convinced a lot of developers could be interested in such functionallity.

I'm very new to xamarin, so I'm hoping you can point me in the right direction if its possible to develop such funcionallity.

Kind regards

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The Xamarin Essentials package includes GeoLocation service. Its an evolution of James Montemegmo's stand-alone GeoLocation service so its well established and countless developers have been using for quite some time.
    So you don't need to re-invent the wheel. Just use what already exists.
    https://docs.microsoft.com/en-us/xamarin/essentials/

  • mivewimivewi DKMember ✭✭

    The geolocation service in Xamarin Essentials gives me the users current location, but I don't think it will continue giving me the users locations when the app is in the background if its not in a background service of some kind and that's my question, how do I make the app continuously give me users location in the even though the user puts the app in the background?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @mivewi said:
    but I don't think it will continue giving me the users locations when the app is in the background

    Have you tried and failed... Or just being pessimistic?

    I have plenty of code that runs when the app is not foreground... logging... server checking for updates... blah blah.
    I have no reason to believe that package won't respond if other C# runs.

  • mivewimivewi DKMember ✭✭
    Hi thanks for answering.

    I'm being pessimistic. I'll give it a try and see what happens and posts the results here.
  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭
    edited May 2

    You can use the location service in the background at least.

    But not while the app is closed on iOS as far as we could tell.

    If you put the subscribing to events from Essentials in a service on Android, you can get that behavior, but I just don't think iOS supports that.

    Last I looked, Essentials didn't run on its own in a background thread, but its pretty simple to do.

    EDIT: Sorry, didnt' realize Essentials was missing things the actual Plugin from James has. Use that one I guess.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    The Xamarin.Essentials Geolocation doesn't appear to have background location tracking included, but James Montemagno's GeolocatorPlugin (which Clint mentioned) does.

  • mivewimivewi DKMember ✭✭
    Oh okay I'll give that a try. Thanks for the info.
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @JoeManke said:
    The Xamarin.Essentials Geolocation doesn't appear to have background location tracking included, but James Montemagno's GeolocatorPlugin (which Clint mentioned) does.

    I may be wrong but.... It may not automatically send location change events while in background... but I think it still responds to direct requests for location. So if you want to get a new location every 1 minute, just ask it on a repeating basis.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @AdamMeaney said:
    EDIT: Sorry, didnt' realize Essentials was missing things the actual Plugin from James has.

    What the... ? Anyone know why that is? Why deliberately lobotomize a working plugin?

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited May 3

    @ClintStLaurent said:
    I may be wrong but.... It may not automatically send location change events while in background... but I think it still responds to direct requests for location. So if you want to get a new location every 1 minute, just ask it on a repeating basis.

    Yes, you could set up a background service to do active tracking (fetch my current location every 1 minute), but Xamarin.Essentials does not has passive tracking (record a new GPS point every time I move 100 feet).

    What the... ? Anyone know why that is? Why deliberately lobotomize a working plugin?

    My guess is it's a limitation of Xamarin.Essentials being all static methods, while James's Geolocator plugin is a singleton object. They didn't want to keep a reference to a native LocationProvider object on a static class to do passive tracking.

  • mivewimivewi DKMember ✭✭
    edited May 6

    I tested James Montemagno's GeolocatorPlugin. In background it shuts down after 5-15 mins. Any of had a similar problem?

    I've tested on a Hauwei P Smart. I know Hauwei is very "good" at closing apps in background, but I came from the cordova world were I used a similar plugin and it was not a problem keeping the app in the background while sending locations on the exactly the same phone.

  • mivewimivewi DKMember ✭✭
    Okay ill get my reading glasses. Thanks a lot.
  • yelinzhyelinzh Member, Xamarin Team Xamurai

    What's the result? Have you made it?

  • mivewimivewi DKMember ✭✭
    Hi

    I used a lot of time on this, but did not have any success. I even asked in the GeolocatorPlugin github forum, they say that I can't do anything about service is not running in the background, it's just the way new phone optimizes the battery life.

    But it can't be true. Coming from the cordova world, I used this plugin: https://github.com/transistorsoft/cordova-background-geolocation-lt and it works in background on the same phone and all other phone for that matter, just out of the box with me not making any settings.

    About your suggestions:

    Your link #2 shows how to implement it in java.

    I have tried to create the service in your #3 link instead.

    Without charger it dies within a couple of minutes in background, but with charger it runs for at least an hour, didn't test it beyond that.

    I only did a little modification to it, as it didn't start with these lines, like claimed:
    var intent = new Intent(ApplicationContext, typeof(PostService));
    var source = PendingIntent.GetBroadcast(ApplicationContext, 0, intent, 0);

    I had to start it like this:
    StartService(new Android.Content.Intent(this, typeof(PostService)));

    So I'm really confused to why it does not want to keep alive without a charger, and I seen it done with the other plugin in cordova.
  • mivewimivewi DKMember ✭✭
    Also what is the meaning of this line:
    var source = PendingIntent.GetBroadcast(ApplicationContext, 0, intent, 0);

    In your link #2 he makes use of an Broadcast receiver to start a new service if old one stops. I think it's the meaning of this also.. But how do I include it?
  • mivewimivewi DKMember ✭✭
    Tried to use the same intent, but still same issue with not running in background:
    var intent = new Intent(ApplicationContext, typeof(PostService));
    var source = PendingIntent.GetBroadcast(ApplicationContext, 0, intent, 0);
    StartService(intent);
  • mivewimivewi DKMember ✭✭

    Also seem to run fine when minimized without charger. Problem is when the display is turned off.

Sign In or Register to comment.