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?
Returns the current enabled/disabled state of location.
Settings > Location > Mode > Battery Saving . This mode only uses WiFi, Bluetooth or mobile data instead of GPS to determine the user location. That's why you have to check if the network provider is enabled and locationManager.
This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup. Requires either of the permissions android.
Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION . The permission you choose determines the accuracy of the location returned by the API. android.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With