0

I'm having a background Service running as a LocationListener on my app.

The idea is to have this service being the only entry point of location updates, and then these are propagated across the app through broadcasts or eventbus.

The problem is, I only receive callbacks when using LocationManager.NETWORK_PROVIDER, and nothing when I use LocationManager.GPS_PROVIDER.

I've seen in this SO ticket that I need to run the code on the main thread to get the callbacks working.

That's why I implemented the following code :

@Override
public void onCreate() {
    super.onCreate();
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mLocationListener = new MyLocationListener();
    if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        mProvider = LocationManager.GPS_PROVIDER;
    } else {
        mProvider = LocationManager.NETWORK_PROVIDER;
    }
    handler = new Handler(getApplicationContext().getMainLooper());
    startOrRestartLocationTracking();
}

private void startOrRestartLocationTracking() {
    handler.post(new Runnable() {
        @Override
        public void run() {
            mLocationManager.removeUpdates(mLocationListener);
            mLocationManager.requestLocationUpdates(mProvider, TIME_INTERVAL, DISTANCE_INTERVAL, mLocationListener);
        }
    });
}

But it still doesn't work...

Any ideas on what I am missing here ?

Community
  • 1
  • 1
nios
  • 1,153
  • 1
  • 9
  • 20
  • If you are indoors, there is a very good chance that you will not be getting gps reception. The phone might not have the accuracy settings set for using gps. I would check both of these and then go outside to see if you can get signal – Chris Handy Jan 13 '15 at 14:51
  • The problem is not related to the GPS signal reception (it works correctly if LocationManager is called from an Activity). – nios Jan 13 '15 at 15:00
  • My mistake, I think we had to use the "Application" context when calling `getSystemService()`. So in the Application class of your app, in the `oncreate()` call `getApplicationContext()`. Then in your service do `MyApplication.getStaticContext().getSystemService(...)` – Chris Handy Jan 13 '15 at 15:09

1 Answers1

0

Application class:

public class MyApplication extends Application {
    public static Context m_appContext = null;

    public void onCreate() {
        MyApplication.m_appContext = getApplicationContext();
    }
}

Service:

@Override
public void onCreate() {
    super.onCreate();
    mLocationManager = (LocationManager) MyApplication.m_appContext.getSystemService(Context.LOCATION_SERVICE);
    mLocationListener = new MyLocationListener();
    if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        mProvider = LocationManager.GPS_PROVIDER;
    } else {
        mProvider = LocationManager.NETWORK_PROVIDER;
    }
    handler = new Handler(getApplicationContext().getMainLooper());
    startOrRestartLocationTracking();

}

Chris Handy
  • 366
  • 1
  • 5