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 ?