Question

I have an IntentService wich should get the user location, and depending of it, download some data from internet. And the problem im facing is that many times, the service is finnished before the method onConnected() gets called. So the Location is null. It doesnt happen all the times, but very often.

I could do it somehow different (like an asynctask), but the IntentService can be called either from an activity (when the user presses a button) or with an AlarmManager (periodically). So i dont know another way to do it. Well, i could create an asynctask that first gets the location, and after, launches the IntentService. And do something different when the Service is called from the AlarmManager. But i would have to create different code for, at the end, do the same thing.

So my question is, is there any way to sort it out, keeping the IntentService? Or is there another better way to do it?

Thanks in adv.

Ps: Ive followed the guide from Android Developers:

https://developer.android.com/intl/es/training/location/retrieve-current.html

Was it helpful?

Solution

Don't use an IntentService - use a regular Service and:

  1. Start listening for location when your Service is started
  2. When you get a location back, start a background thread to download your data (and stop location updates if you only want a single location)
  3. Call stopSelf once your download is complete to stop the service

OTHER TIPS

Hi what I recommend in your case... is create a service that will be updating the current location... and you charge your var´s with the coordinates... let me show you an example

first you declare the variables

public class MyService extends Service{
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
String sLat;
String sLong;

and then on your on locationChange method you charge sLat and sLong

public void onLocationChanged(Location location)
    {
        sLat = Double.toString(location.getLatitude());
        sLong = Double.toString(location.getLongitude());

so sLat and sLong they are going to have the location and they will be available whenever you want...

regards...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top