Skip to content Skip to sidebar Skip to footer

Android Detect Location Settings

I'm using the new Location API in my app and I'm wondering how to detect if I the user has location access enabled on there phone. I'm setting up a LocationClient and running a Loc

Solution 1:

In your onConnected you could call a method like these:

private boolean checkLocationProviders()
{
    if(mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
    {
        returntrue;
    }
    else
    {
        if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            returntrue;
        elsereturnfalse;
    }
}

If false is returned, you can open the Settings screen in this way:

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

Solution 2:

This will help you

// flag for GPS statusbooleanisGPSEnabled=false;

// flag for network statusbooleanisNetworkEnabled=false;

// flag for GPS statusbooleancanGetLocation=false;

Locationlocation=null; // locationdouble latitude; // latitudedouble longitude; // longitude// The minimum distance to change Updates in metersprivatestaticfinallongMIN_DISTANCE_CHANGE_FOR_UPDATES=10; // 10 meters// The minimum time between updates in millisecondsprivatestaticfinallongMIN_TIME_BW_UPDATES=1000 * 60 * 1; // 1 minute// Declaring a Location Managerprotected LocationManager locationManager;

 public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

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

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

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network Enabled");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Servicesif (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

Post a Comment for "Android Detect Location Settings"