shernandezp / XamarinForms.LocationService

MAUI Background Services, Background Location Updates, Location Updates, BroadCastReceiver
https://github.com/shernandezp
Apache License 2.0
71 stars 25 forks source link

Background Service sleeps when screen is locked #11

Closed gpsaliola closed 2 years ago

gpsaliola commented 2 years ago

Describe the bug The background service returns coordinates until the screen is locked. During the locked screen the service seems to sleep. After screen is unlocked the service restart to return coordinates.

To Reproduce Steps to reproduce the behavior: Add Location: Allow always to the app permissions. In Location.cs add a statement (WriteLog) to write timestamp to file

        public async Task Run(CancellationToken token)
        {
            await Task.Run(async () => {
                while (!stopping)
                {
                    token.ThrowIfCancellationRequested();
                    try
                    {
                        await Task.Delay(2000);

                        var request = new GeolocationRequest(GeolocationAccuracy.High);
                        var location = await Geolocation.GetLocationAsync(request);
                        if (location != null)
                        {
                            var message = new LocationMessage 
                            {
                                Latitude = location.Latitude,
                                Longitude = location.Longitude
                            };

                            WriteLog(location.Timestamp.ToString());

                            Device.BeginInvokeOnMainThread(() =>
                            {
                                MessagingCenter.Send<LocationMessage>(message, "Location");
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            var errormessage = new LocationErrorMessage();
                            MessagingCenter.Send<LocationErrorMessage>(errormessage, "LocationError");
                        });
                    }
                }
                return;
            }, token);
        }

        private void WriteLog(string msg)
        {
            string fileLog = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "log.log");
            using (System.IO.StreamWriter sw = System.IO.File.AppendText(fileLog))
            {
                sw.WriteLine(msg);
            }
        }

Expected behavior Run app and lock the screen. After 10 minutes unlock the screen and close the app. Connect the iPhone to PC and copy the log file that has been created and analyze the content. You will find a hole in the period that the screen was locked.

Desktop:

Smartphone:

gpsaliola commented 2 years ago

Why this post hes been closed without any answer?