How to keep gettion location when application is in background? #15190
Replies: 6 comments 5 replies
-
If the platform doesn't let you what are you going to do, for example if Apple refuse letting applications get location in background because we are at war with spyware or china |
Beta Was this translation helpful? Give feedback.
-
In my case the application is an Android application that runs in Android 12. |
Beta Was this translation helpful? Give feedback.
-
Try this, I think it was the thing that helped me (if you use Android): https://github.com/jfversluis/XFBackgroundLocationSample |
Beta Was this translation helpful? Give feedback.
-
@janseris Thanks for the example. I have tried this code and it happens the same to me. If I put the application in the backgroud or I close it, the location of the GPS is not requested. If I open the application again, then it continues requesting the location. If we focus in the code of the location service:
I have realized that if I set a breakpint in the delay of 2 seconds, if I set the application in background or I stop it, the code is not run, although i still can see the icon in the state bar that the foreground services is running. I have tried to don't give to the application permission to can use the location. In this case, in this code, the exception is thrown. I have realized that in this case, if I put the application in the background or I close the application, the code is still running. I know it because of the breakpint in the delay of 2 seconds. So it works in the same way than in my code. If the application has permissions to use the location, the code is not run when the application is not in the foreground, although the foreground services is still running. However, if the location is no request, for example because an expcetion is thrown, the the code is running always, no matter if the application is in the background or not. |
Beta Was this translation helpful? Give feedback.
-
Yes, the first notification that shows that the foreground is running it is shown. It is not possible to create and start a fororeground without a notification. I know that this first notification is created it is because in the status bar I can see the icon of my application and also I can see the notification in the notifications panel. I know that the location is requested because every time that the location is requested, an arrow icon is shown in the icons beside the time, in the upper right corner. So this icon is show every 2 seconds (the interval of the retrieving). How ever, this icon is not shown when I put the application in the backgroud or I close it. Also I know that the location is not request when the application is not open because if i set a breakpoint in code that request the location, this breakpoint is not reached. |
Beta Was this translation helpful? Give feedback.
-
@HaimoHeymann I have created a repository with the code: repository. The repository is just the code of classes that make it possible, but it is not complete, because it is from a project that I can't share the whole code. Anyway, it shows the part of code that you need. As important points, the code shows how to use a foreground that use a timer inside a while to request the location periodically. It is in the class "ServicioAlarmas.cs". Also the codes shows how to start the foreground when the device is rebooted. For that, you could check the code in the \platforms\Android directory. There are another helpers, for example, the helper that request the location. I hope this classes work. Any question that you could have, tell me and I will try to help you. I hope this code can help you. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to have an application that get the location periodically, every 5 minutes for example, compare this location with some locations that I have stored (points of interest) and if the distance is less than x meters, then show a notification.
In the android documentation: https://developer.android.com/guide/components/foreground-services
I have read that the foreground it is an option for applications likea music player. The music player is not running in the foreground most of the time, and is in the notification bar where you can see the song that is playing and another information.
Because of that, I would try to use a foreground service to see if it is a good option for me. This is the application:
MauiForegroundServiceWithGps.zip
the main code is in the AlarmService.cs file, in the method that do the work:
The problem with this is that when the application is in the background, this method stops. However, if I change the code of this method, intestad of request the location, for someting like this, it works if I put the application in the backgroud or if I close the application.
So in general, the background can work as I expect, run although the application is in background or closed, but when I request the location it seems the bahaviour is different.
So my question is, how could I keep requesting the location although the application is in background or closed?
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions