So Waze and we used FusedLocation in past and faced a lot of problems.
The API is broken. There are some devices that not perf it properly. Activity recognition that fused a lot relay not working and recognize staff properly.
We end, to switching back to location manager API.
Here the snippet:
Init location manager and start to request the updates on different thread.
I personally register separate listeners for each provider
if (mLocationManager == null) {
LOGGER.info("Location track,start called first time. Creating Location Manager");
mLocationManager = (LocationManager) mContextWeakReference.get()
.getSystemService(Context.LOCATION_SERVICE);
mLocationHandlerThread
= new HandlerThread("LocationThread", Thread.NORM_PRIORITY);
mLocationHandlerThread.start();
// Now get the Looper from the HandlerThread
// NOTE: This call will block until the HandlerThread gets control and initializes its Looper
Looper looper = mLocationHandlerThread.getLooper();
Location networkLastKnownLocation = null;
Location gpsLastKnownLocation = null;
mGpsLocationListener = new GpsLocationListener();
mNetworkLocationListener = new NetworkLocationListener();
// Register the listener with the Location Manager to receive location updates
if (mLocationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER)) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
LOCATION_REQUEST_INTERVAL_MILLIS, SMALLEST_DISPLACEMENT_METERS,
mGpsLocationListener,
looper);
gpsLastKnownLocation = mLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} else {
setGpsProviderAvailable(false);
}
if (mLocationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)) {
networkLastKnownLocation = mLocationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
LOCATION_REQUEST_INTERVAL_MILLIS, SMALLEST_DISPLACEMENT_METERS,
mNetworkLocationListener,
looper);
}
setLastKnownLocationDifferentProviders(gpsLastKnownLocation,
networkLastKnownLocation);
}
And the listeners are:
public class GpsLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
LOGGER.info("Location track ,onLocationChanged location={}", location);
switch (location.getProvider()) {
case LocationManager.GPS_PROVIDER:
if (mLocationManager != null) {
mLocationManager.removeUpdates(mNetworkLocationListener);
}
break;
}
setLastKnownLocation(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
LOGGER.info("onStatusChanged: {}, status: {}", provider, status);
}
@Override
public void onProviderEnabled(String provider) {
LOGGER.info("onProviderEnabled: " + provider);
if (provider.equals(LocationManager.GPS_PROVIDER)) {
setGpsProviderAvailable(true);
}
}
@Override
public void onProviderDisabled(String provider) {
LOGGER.info("onProviderDisabled: " + provider);
if (provider.equals(LocationManager.GPS_PROVIDER)) {
setGpsProviderAvailable(false);
}
}
}
public class NetworkLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
LOGGER.info("Location track ,onLocationChanged location={}", location);
switch (location.getProvider()) {
case LocationManager.NETWORK_PROVIDER:
setLastKnownLocation(location);
break;
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
LOGGER.info("onStatusChanged: {}, status: {}", provider, status);
}
@Override
public void onProviderEnabled(String provider) {
LOGGER.info("onProviderEnabled: " + provider);
if (provider.equals(LocationManager.GPS_PROVIDER)) {
setGpsProviderAvailable(true);
}
}
@Override
public void onProviderDisabled(String provider) {
LOGGER.info("onProviderDisabled: " + provider);
if (provider.equals(LocationManager.GPS_PROVIDER)) {
setGpsProviderAvailable(false);
}
}
}
Don't forgot to unregister and stop all threads :)