How to always get location in Background Task with Xamarin.Forms?

Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

hi everybody!
I'm creating an app to using GPS and calculate distance running
How to always get current location in background task with Xamarin.Forms?
Thank you!

Tagged:

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    https://github.com/acaliaro/TestThread

    you can use a thread...

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    Thank you, I'll read it to night. How frequency thread runnning?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    you can decide

                    await Task.Delay(1000 , App.CancellationToken.Token).ContinueWith(async (arg) =>{
    

    this is 1 second

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    Thank you!
    Does it will drain devices battery?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Virtually the same question and need and advice as this thread from just a couple days ago.
    https://forums.xamarin.com/discussion/comment/264765#Comment_264765

    I'm sure the help there can help you as well.

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭
    > @AlessandroCaliaro said:
    > I think it depends on your frequency. If you use GPS you can take a look to https://components.xamarin.com/view/geolocatorplugin
    >
    > It should have an event that raise every time it change the coordinates...
    >
    > async Task StartListening() { await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(5), 10, true, new Plugin.Geolocator.Abstractions.ListenerSettings { ActivityType = Plugin.Geolocator.Abstractions.ActivityType.AutomotiveNavigation, AllowBackgroundUpdates = true, DeferLocationUpdates = true, DeferralDistanceMeters = 1, DeferralTime = TimeSpan.FromSeconds(1), ListenForSignificantChanges = true, PauseLocationUpdatesAutomatically = false }); CrossGeolocator.Current.PositionChanged += Current_PositionChanged; } private void Current_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e) { Device.BeginInvokeOnMainThread(() => { var test = e.Position; listenLabel.Text = "Full: Lat: " + test.Latitude.ToString() + " Long: " + test.Longitude.ToString(); listenLabel.Text += "\n" + $"Time: {test.Timestamp.ToString()}"; listenLabel.Text += "\n" + $"Heading: {test.Heading.ToString()}"; listenLabel.Text += "\n" + $"Speed: {test.Speed.ToString()}"; listenLabel.Text += "\n" + $"Accuracy: {test.Accuracy.ToString()}"; listenLabel.Text += "\n" + $"Altitude: {test.Altitude.ToString()}"; listenLabel.Text += "\n" + $"AltitudeAccuracy: {test.AltitudeAccuracy.ToString()}"; }); }

    GeolocatorPlugin sample do not run in background
  • JohnHardmanJohnHardman GBUniversity mod

    @NguyenLiem - It requires extra effort to use the Geolocator plugin from background.

    The documentation at https://github.com/jamesmontemagno/GeolocatorPlugin says:

    "Listening for Location Changes

    iOS has special capabilities that allows certain types of apps to get location updates when in the background, but you must specify this.
    On Android you should use a background service and bind to the UI
    Windows you should also use some background services"

    and:

    "iOS 9 Special Case: Background Updates (for background agents, not background tasks):"

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭
    How to make it work?> @JohnHardman said:
    > @NguyenLiem - It requires extra effort to use the Geolocator plugin from background.
    >
    > The documentation at https://github.com/jamesmontemagno/GeolocatorPlugin says:
    >
    > "Listening for Location Changes
    >
    > iOS has special capabilities that allows certain types of apps to get location updates when in the background, but you must specify this.
    > On Android you should use a background service and bind to the UI
    > Windows you should also use some background services"
    >
    > and:
    >
    > "iOS 9 Special Case: Background Updates (for background agents, not background tasks):"

    How to make it work?
  • JohnHardmanJohnHardman GBUniversity mod

    @NguyenLiem - For how to implement backgrounding, take a look at http://arteksoftware.com/backgrounding-with-xamarin-forms/

    If you have access, watch the video by @RobGibbens at https://university.xamarin.com/lightninglectures

    There's a load more stuff on Xamarin University if you have access.

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    Please help, How to get background location in long time base on this sample
    Thank you!

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Have you looked around the forum at all the other topics for the same question?
    There's lots of good information here if you just do a search.
    https://forums.xamarin.com/discussion/93788/best-way-to-handle-location-maps

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @ClintStLaurent said:
    Have you looked around the forum at all the other topics for the same question?
    There's lots of good information here if you just do a search.
    https://forums.xamarin.com/discussion/93788/best-way-to-handle-location-maps

    I have 2 button in a view page of Portable project. How iOS, Android and UWP know when I press start button to start background task and when I press stop button to stop it?

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭
    > @AlessandroCaliaro said:
    > use CancellationToken?
    >
    > https://msdn.microsoft.com/it-it/library/dd997396(v=vs.110).aspx

    Do we have any ways else?
  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @AlessandroCaliaro said:
    I think it depends on your frequency. If you use GPS you can take a look to https://components.xamarin.com/view/geolocatorplugin

    It should have an event that raise every time it change the coordinates...

    async Task StartListening()
    {
    await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(5), 10, true, new Plugin.Geolocator.Abstractions.ListenerSettings
    {
    ActivityType = Plugin.Geolocator.Abstractions.ActivityType.AutomotiveNavigation,
    AllowBackgroundUpdates = true,
    DeferLocationUpdates = true,
    DeferralDistanceMeters = 1,
    DeferralTime = TimeSpan.FromSeconds(1),
    ListenForSignificantChanges = true,
    PauseLocationUpdatesAutomatically = false
    });

    CrossGeolocator.Current.PositionChanged += Current_PositionChanged;
    }

    private void Current_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
    {
    Device.BeginInvokeOnMainThread(() =>
    {
    var test = e.Position;
    listenLabel.Text = "Full: Lat: " + test.Latitude.ToString() + " Long: " + test.Longitude.ToString();
    listenLabel.Text += "\n" + $"Time: {test.Timestamp.ToString()}";
    listenLabel.Text += "\n" + $"Heading: {test.Heading.ToString()}";
    listenLabel.Text += "\n" + $"Speed: {test.Speed.ToString()}";
    listenLabel.Text += "\n" + $"Accuracy: {test.Accuracy.ToString()}";
    listenLabel.Text += "\n" + $"Altitude: {test.Altitude.ToString()}";
    listenLabel.Text += "\n" + $"AltitudeAccuracy: {test.AltitudeAccuracy.ToString()}";
    });
    }

    I have follow this PlugIn, but on UWP when minimize app location stop. how to make it continues run when minimize app

  • brux88brux88 USMember ✭✭
    edited September 2018

    @AlessandroCaliaro said:
    I think it depends on your frequency. If you use GPS you can take a look to https://components.xamarin.com/view/geolocatorplugin

    It should have an event that raise every time it change the coordinates...

    async Task StartListening()
    {
    await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(5), 10, true, new Plugin.Geolocator.Abstractions.ListenerSettings
    {
    ActivityType = Plugin.Geolocator.Abstractions.ActivityType.AutomotiveNavigation,
    AllowBackgroundUpdates = true,
    DeferLocationUpdates = true,
    DeferralDistanceMeters = 1,
    DeferralTime = TimeSpan.FromSeconds(1),
    ListenForSignificantChanges = true,
    PauseLocationUpdatesAutomatically = false
    });

    CrossGeolocator.Current.PositionChanged += Current_PositionChanged;
    }

    private void Current_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
    {
    Device.BeginInvokeOnMainThread(() =>
    {
    var test = e.Position;
    listenLabel.Text = "Full: Lat: " + test.Latitude.ToString() + " Long: " + test.Longitude.ToString();
    listenLabel.Text += "\n" + $"Time: {test.Timestamp.ToString()}";
    listenLabel.Text += "\n" + $"Heading: {test.Heading.ToString()}";
    listenLabel.Text += "\n" + $"Speed: {test.Speed.ToString()}";
    listenLabel.Text += "\n" + $"Accuracy: {test.Accuracy.ToString()}";
    listenLabel.Text += "\n" + $"Altitude: {test.Altitude.ToString()}";
    listenLabel.Text += "\n" + $"AltitudeAccuracy: {test.AltitudeAccuracy.ToString()}";
    });
    }

    @AlessandroCaliaro
    hi, i have this problem with xamarin fomr ios platform:
    https://forums.xamarin.com/discussion/138882/xamarin-form-ios-not-work-background-mode-and-kill-app#latest

    i hope you help me

  • kotetotemkotetotem Member ✭✭

    @AlessandroCaliaro said:

    It should have an event that raise every time it change the coordinates...

    async Task StartListening()
    {
    await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(5), 10, true, new Plugin.Geolocator.Abstractions.ListenerSettings
    {
    ActivityType = Plugin.Geolocator.Abstractions.ActivityType.AutomotiveNavigation,
    AllowBackgroundUpdates = true,
    DeferLocationUpdates = true,
    DeferralDistanceMeters = 1,
    DeferralTime = TimeSpan.FromSeconds(1),
    ListenForSignificantChanges = true,
    PauseLocationUpdatesAutomatically = false
    });

    CrossGeolocator.Current.PositionChanged += Current_PositionChanged;
    }

    private void Current_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
    {
    Device.BeginInvokeOnMainThread(() =>
    {
    var test = e.Position;
    listenLabel.Text = "Full: Lat: " + test.Latitude.ToString() + " Long: " + test.Longitude.ToString();
    listenLabel.Text += "\n" + $"Time: {test.Timestamp.ToString()}";
    listenLabel.Text += "\n" + $"Heading: {test.Heading.ToString()}";
    listenLabel.Text += "\n" + $"Speed: {test.Speed.ToString()}";
    listenLabel.Text += "\n" + $"Accuracy: {test.Accuracy.ToString()}";
    listenLabel.Text += "\n" + $"Altitude: {test.Altitude.ToString()}";
    listenLabel.Text += "\n" + $"AltitudeAccuracy: {test.AltitudeAccuracy.ToString()}";
    });
    }

    Hi, so StartListening() works great, but I am having problems biniding it so it can trigger itself every few meter or a seconds...
    I have read documentation for Geoloator plugin, but I still dont understand how to call "StartListening()" method every few seconds/meters...

    Pls can you help me, do you have any example of a code?

    Or do I need to run foreground services?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    I think you should take a look to Xamarin Essentials . There should be a new class for geolocalization
Sign In or Register to comment.