Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get location or prompt to enable location service if disabled

I've found bits and pieces of this answer scattered through other posts, but I wanted to record it here for others.

How can I simply request the user's GPS and/or Network location and, if they haven't enabled the service, prompt them to do so?

like image 250
DougW Avatar asked Aug 12 '10 18:08

DougW


People also ask

How do I force my location on Android?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.

How do you force a location?

GPS Only Mode on Galaxy Devices Go to “Settings”. Scroll down dive into “Location”. First, toggle on the location. Then, open the “Improve accuracy” settings.

Which permissions are required to get a location in Android?

Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION .


2 Answers

If you want to capture the location on a button push, here's how you'd do it. If the user does not have a location service enabled, this will send them to the settings menu to enable it.

First, you must add the permission "android.permission.ACCESS_COARSE_LOCATION" to your manifest. If you need GPS (network location isn't sensitive enough), add the permission "android.permission.ACCESS_FINE_LOCATION" instead, and change the "Criteria.ACCURACY_COARSE" to "Criteria.ACCURACY_FINE"

Button gpsButton = (Button)this.findViewById(R.id.buttonGPSLocation);
gpsButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // Start loction service
        LocationManager locationManager = (LocationManager)[OUTERCLASS].this.getSystemService(Context.LOCATION_SERVICE);

        Criteria locationCritera = new Criteria();
        locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
        locationCritera.setAltitudeRequired(false);
        locationCritera.setBearingRequired(false);
        locationCritera.setCostAllowed(true);
        locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);

        String providerName = locationManager.getBestProvider(locationCritera, true);

        if (providerName != null && locationManager.isProviderEnabled(providerName)) {
            // Provider is enabled
            locationManager.requestLocationUpdates(providerName, 20000, 100, [OUTERCLASS].this.locationListener);
        } else {
            // Provider not enabled, prompt user to enable it
            Toast.makeText([OUTERCLASS].this, R.string.please_turn_on_gps, Toast.LENGTH_LONG).show();
            Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            [OUTERCLASS].this.startActivity(myIntent);
        }
    }
});

My outer class has this listener set up

private final LocationListener locationListener = new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        [OUTERCLASS].this.gpsLocationReceived(location);
    }

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}

};

Then, whenever you want to stop listening call this. You should at least make this call during your activity's onStop method.

LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(this.locationListener);
like image 182
DougW Avatar answered Nov 04 '22 08:11

DougW


After going through a lot of answers on Stack Overflow, I found this method to work perfectly fine and doesn't even require a lot of code.
Declare int GPSoff = 0 as a global variable.
Now wherever you need to check for the present status of the GPS and re-direct the user to turn the GPS on, use this :

try {
                GPSoff = Settings.Secure.getInt(getContentResolver(),Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
            }
            if (GPSoff == 0) {
                showMessageOKCancel("You need to turn Location On",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent onGPS = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                startActivity(onGPS);
                            }
                        });
            }
like image 35
Yankee Avatar answered Nov 04 '22 08:11

Yankee