jgelderloos
jgelderloos

Reputation: 388

getting location from a background service, is this correct?

I have a background service that is getting updates from the location manager every 5 minutes. When on locationChaged is called, I call a function in my activity to update the GeoPoint that holds my user location. This is working fine and does what I want. I am just unsure if it is better to use something like broadcast receivers to do this. I have tried using a broadcast receiver but it is not working. Is there anything incorrect with the way I am currently doing this?

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 bound activity 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));
}

Upvotes: 4

Views: 418

Answers (1)

Mike L.
Mike L.

Reputation: 589

What you are doing is fine and from my experience the standard way of dealing with location updates. It is very easy to configure the LocationManager to request updates every 5 minutes. My only suggestion would be to think if you really need a service to do what you need. Do you only need location updates while your activity is running on the screen? If so do not use a background service. If you need updates to occur even when your activity has been closed then use a service and make sure to put up an OnGoing notification so the user knows your service is running in the background.

Broadcast receivers are used more for when you need to start or stop a background service after certain events on the phone such as after the phone has turned on.

Upvotes: 2

Related Questions