Elad Benda
Elad Benda

Reputation: 36654

onLocationChanged isn't called even though the GPS provider is availble

I try to get the device current location using MyCustomLocationListener.

My code passes here and indicated GPS and Network providers are both available.

// getting GPS status
isGPSEnabled = androidLocationManager
        .isProviderEnabled(LocationManager.GPS_PROVIDER);

// getting network status
isNetworkEnabled = androidLocationManager
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isGPSEnabled && !isNetworkEnabled) {
    // no network provider is enabled
    Log.e(MyLogger.TAG, "Non providers are enabled");

    showEnableGpsDialog();
    return;
}

if (isGPSEnabled) {
    androidLocationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 0, 0,
            saveToSharedPrefListener);
    Log.d(MyLogger.TAG, "GPS Enabled");
}

if (isNetworkEnabled) {
    if (androidLocationManager == null) {
        androidLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0,
                saveToSharedPrefListener);
        Log.d(MyLogger.TAG, "Network Enabled");
    }
}

but then the onLocationChanged is never called when I'm indoor.

Only when I use a Fake GPS app, the onLocationChanged is called.

How can I write a fallback (with\out timeout) to force invoke onLocationChanged

I used to use another API requestSingleUpdate :

What is the difference between these two in a matter of working indoor?

if (isGPSEnabled) {
    androidLocationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 0, 0,
            saveToSharedPrefListener);
    Log.d(MyLogger.TAG, "GPS Enabled");
}

and

if (BaseApplication.getCurrentActivity() != null) {
    locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER,
            saveToSharedPrefListener, BaseApplication.getCurrentActivity()
            .getMainLooper());

Upvotes: 3

Views: 700

Answers (2)

biegleux
biegleux

Reputation: 13247

I assume network location provider is never requested for location updates, according to your null-check in the code, otherwise you would see an exception:

if (isNetworkEnabled) {
    if (androidLocationManager == null) {
        androidLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0,
                saveToSharedPrefListener);
        Log.d(MyLogger.TAG, "Network Enabled");
    }
}

Upvotes: 1

Batuhan Coşkun
Batuhan Coşkun

Reputation: 2969

  • but then the onLocationChanged is never called when I'm indoor.

You can not do anything about that. But, some phones can get the location indoor, it is related to the phone's GPS Receiver, so hardware problem.

Then if you can understand that you are indoor, you can use LocationManager.NETWORK_PROVIDER for getting the location. But, it works with higher accuracy then LocationManager.GPS_PROVIDER.

  • What is the difference between these two in a matter of working indoor?

    1. If your GPS Receiver could not get location updates(sometimes indoor, sometimes bad weather, also with respect to your phone's hardware), it does not matter which block you use.
    2. If your GPS Receiver can get location updates, then use the first one to updating your location continuously.

Note that: "GPS provider is available" does not mean that you can get location with it. Unreasonable but on android devices the case is like that.

I wish that helps.

Upvotes: 2

Related Questions