0

I'm developing an application which needs to retrieve user location for periods of time. I have used LocationListener interface in activities and it has worked fine, however when I implement it in my android Service it isn't called. Here's the onCreate method:

public void onCreate() {
    super.onCreate();
    turned= true;       
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    locationManager = (LocationManager) this
            .getSystemService(LOCATION_SERVICE);
    String provider = locationManager.getBestProvider(criteria, true);
    locationManager
            .requestLocationUpdates(provider, 0, 0, this); ...}

I would really appreciate any help, Service seems fine because I send Toast and they are sent, but onLocationUpdate is never called.

  • Are you sure it's actually got a GPS lock? It can take a while. Reference: http://stackoverflow.com/questions/5337333/android-onlocationupdate-not-called-with-gps-provider – Justin Jasmann Apr 24 '13 at 17:33
  • I call this inside onStartCommand() instead of OnCreate() and it works for me. You might want to give it a try. – Shobhit Puri Apr 24 '13 at 17:36
  • Thanks a lot Shobhit Puri, your answer solved my problem. – user1998791 Apr 24 '13 at 19:26
  • Location listener works OK in service. 1. Check if you have got GPS fix. 2. How do you implement LocationListener? – piotrpo Apr 25 '13 at 15:51

1 Answers1

0

EDIT: As Shobhit Puri said, it just need to be in the onStartCommand()

public int onStartCommand (Intent intent, int flags, int startId)
{
    super.onCreate();
    encendido = true;       
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    locationManager = (LocationManager) this
            .getSystemService(LOCATION_SERVICE);
    String provider = locationManager.getBestProvider(criteria, true);
    locationManager
            .requestLocationUpdates(provider, 0, 0, this);
    return START_STICKY;
}