1

I'm trying to make an Android app that gets (and saves) GPS coordinates every time a questionnaire form on the app is completed and the Save button is pressed (noting the GPS location part is not shown). Minimum device platform requirement is Oreo 8.1 (API level 27).

I have looked up two location-samples available from Android's Github:

  1. Foreground Location Updates
  2. Background Location Updates

But on Android's Site, in Services, it says:

In most situations, for example, you shouldn't access location information from the background. Instead, schedule tasks using WorkManager.

Should I go for the 'Foreground' sample's approach?

(Also, in the README.md file, for the above mentioned github-location-samples, it lists 'Basic Location' samples (in Java and Kotlin) but the links take you to no resource/code.)

Can you kindly guide me on which approach to take?

codeontime
  • 11
  • 2
  • Foreground approach is preferred because background one is blocked by Google Play and requires special conformition. – guest Aug 02 '22 at 12:28
  • By background, you mean you get the location data even if the application is not used by the user ( eg. screen is off/ the user has other app open )? Or you mean that the app is open, and you just get the location data without telling the user directly? – Ionut Aug 02 '22 at 12:33
  • Key point is you can't publish an app with user-permission as android.permission.BACKGROUND_SERVICE without special approval from Google Play. – guest Aug 02 '22 at 12:57
  • @guest My question was for OP. I am not the one that asked the question :). – Ionut Aug 02 '22 at 12:58
  • @lonut Thank you for responding. By background I mean it's not shown to the user (in a text-field or some notification) but is noted by the app (just like photos are location-tagged). The people using the questionnaire know that the GPS is noted (the people running the app are hired (or volunteer) to get answers from other people). The location needs to be noted at the end of the questionnaire, once the question-form is filled and the 'Save' button is pressed. – codeontime Aug 29 '22 at 14:59
  • @guest Thank you for responding. I didn't know that this permission is required for background service. I think foreground location method is better. I will try to use that. – codeontime Aug 29 '22 at 15:01
  • schedule periodic workmanager in order to run in background in workmanager get user location. – karan Jul 07 '23 at 11:43

1 Answers1

0

If you don't need the location to be updated repeatedly in the background you could as well use FusedLocationProviderClient and it's getCurrentLocation() method to get the current location of the device. This would reduce the complexity of the code compared to a solution using background location updates. Alternatively you can also use getLastLocation() - you can read about the differences under the provided links.

See: Get the last known location (Android documentation)

Notes:

  • fused location provider requires Google Play services, so it may not be available on all devices, depending on what you are targeting
  • "Foreground" in this context means that a foreground service will be visible to the user (notification in the status bar).
Klemens Zleptnig
  • 1,698
  • 20
  • 36