rgr_mt
rgr_mt

Reputation: 985

LocationManager: is the "network" provider always enabled?

I want to select a LocationProvider that is enabled in Android. The project build target is Android 2.1.

This is what I do in onCreate().

// ...
LocationManager locationMgr = (LocationManager) 
getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setCostAllowed(false);     

String bestProvider = locationMgr.getBestProvider(criteria, true);  

Toast.makeText(getApplicationContext(),   "Provider = " + bestProvider + " enabled= " + locationMgr.isProviderEnabled(bestProvider), Toast.LENGTH_LONG).show();
// ...

Now, I switch each network interface off and set flight mode on my device (HTC Desire, Android 2.2). I disconnect the device from USB. There is clearly no provider alive who could actually provide location data to the device. I specifically ask getBestProvider for enabled providers only, so I expect it to return null or an empty string in that case. I expect isProviderEnabled to return false.

The actual result is that getBestProvider returns "network" and isProviderEnabled reports it to be "enabled". Is "network" always "enabled" even when its not?

Upvotes: 18

Views: 22563

Answers (3)

fergerm
fergerm

Reputation: 241

You always get true as an answer because you have selected the "Use networks" option in the settings menu. I also had this problem until I found this out. I hope you find this useful.

Upvotes: 1

rgr_mt
rgr_mt

Reputation: 985

After some digging I can answer my own question. First I tried airplane mode with:

ConnectivityManager connectivityMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo[] nwInfos = connectivityMgr.getAllNetworkInfo();
for (NetworkInfo nwInfo : nwInfos) {
  Log.d(TAG, "Network Type Name: " + nwInfo.getTypeName());
  Log.d(TAG, "Network available: " + nwInfo.isAvailable());
  Log.d(TAG, "Network c_or-c: " + nwInfo.isConnectedOrConnecting());
  Log.d(TAG, "Network connected: " + nwInfo.isConnected());
} 

The ConnectivityManager reports correctly "false" since there is no connection. This is useful to check if you actually have a network and therefore a network-based location provider available. Then I took a second look at my device settings. And here is the answer:

locationMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

reports if the user has checked the device setting (in my case under Location - My Location). If you uncheck all providers there it does return null as expected. It is actually documented in isProviderEnabled() but I must have overlooked it . Case closed.

Upvotes: 21

Ollie C
Ollie C

Reputation: 28509

Try this

public static boolean isLocationSensingAvailable()
{
    boolean hasActiveLocationProvider = false;
    List<String> providers = locationManager.getProviders(true);
    for (String providerName:providers)
    {
        if (providerName.equals(LocationManager.GPS_PROVIDER))
        {
            hasActiveLocationProvider = isLocationProviderEnabled(providerName);
        }
        if (providerName.equals(LocationManager.NETWORK_PROVIDER))
        {
            hasActiveLocationProvider = ( SpondleApplication.isOnline() &&  isLocationProviderEnabled(providerName));
        }
    }
    return hasActiveLocationProvider;
}

Upvotes: 1

Related Questions