Getting location from background service, is this correct?

I have a help desk that receives updates from the location manager every 5 minutes. When onChaged is called, I call a function in my activity to update GeoPoint, which contains my user location. It works great and does what I want. I'm just not sure if it’s better to use something like broadcast receivers. I tried using a broadcast receiver, but it does not work. Is there something wrong with the way I'm doing it now?

I have the following methods in my location service:

public static double getLat() { return lat; } public static double getLng() { return lng; } public void onLocationChanged(Location loc) { lat = (double) (loc.getLatitude()); lng = (double) (loc.getLongitude()); mapview.locationHasChanged(); } 

And in my related activities, I have the following:

 public static void locationHasChanged() { double myLat = LocationService.getLat(); double myLng = LocationService.getLng(); LocGeo = new GeoPoint( (int) (myLat * 1E6), (int) (myLng * 1E6)); } 
+4
source share
1 answer

What you do is fine and in my experience is the standard way to solve location update problems. It is very easy to configure LocationManager for update requests every 5 minutes. My only suggestion is to think about whether you really need a service to do what you need. Do you only need location updates while working on the screen? If so, do not use the background service. If you need updates, even when your activity has been closed, use the service and do not forget to add an OnGoing notification so that the user knows that your service is running in the background.

Broadcast receivers are used more when you need to start or stop the background service after certain events on the phone, for example, after turning on the phone.

+2
source

Source: https://habr.com/ru/post/1383328/


All Articles