Search code examples
javaandroidconnectionwifi

How to check programmatically if "use wireless networks" is enabled on Android?


I don't want to check if there is any connectivity over WIFI or 3G! I just want to verify (and not change!) if the "Use wireless networks"-CheckBox is enabled (the one you can set in Settings > Location and security > My location > HERE) Thanx


Solution

  • Code from RajaReddy will check if Wifi is enabled for network. If you want to know if Wifi is enabled for positioning then this should do (replace "context" with something useful)

    ContentResolver cr = context.getContentResolver();
    boolean wifiEnabled = Settings.Secure.isLocationProviderEnabled(cr, LocationManager.NETWORK_PROVIDER);
    

    example code for API Level < 8

    private static boolean isWifiLocationEnabled (Context context) {
        ContentResolver cr = context.getContentResolver();
        String enabledProviders = Settings.Secure.getString(cr, Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if (!TextUtils.isEmpty(enabledProviders)) {
            // not the fastest way to do that :)
            String[] providersList = TextUtils.split(enabledProviders, ",");
            for (String provider : providersList) {
                if (LocationManager.NETWORK_PROVIDER.equals(provider)) {
                    return true;
                }
            }
        }
        return false;
    }