Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 762
rgr_mt Avatar asked Feb 05 '11 15:02

rgr_mt


People also ask

Which of the following location provider is returned by LocationManager?

Returns the current enabled/disabled state of location.

How do I know if android location is enabled?

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.

What is network provider in android Studio?

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.

What are the required permission for android Location Manager?

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.


2 Answers

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.

like image 166
rgr_mt Avatar answered Oct 05 '22 03:10

rgr_mt


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;
}
like image 42
Ollie C Avatar answered Oct 05 '22 02:10

Ollie C