I remember the “good ol’ days” of location-based programming where there was a simple API call that allowed an application to enable or disable a mobile device’s GPS service. Today, user privacy and security concerns make that sort’a thing no longer possible.
In today’s world, a phone’s user generally has ultimate control of what features are and are not enabled on their phone. Although I agree this behavior is best for users, it definitely creates headaches for developers.
The one thing we know we can’t do as developers, is display a message that simply says “Hey! Go turn on your GPS”. Seeing a message like that would cause most users to uninstall our app and look for an app that’s easier to use.
The good news is that as developers we can send the user directly to the location settings screen right from within our app with a simple Intent.
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent);
The exact format of the screen the user sees will vary based on their Android version and device model, but the above code will always take them to the appropriate settings screen. On my Samsung device, the ACTION_LOCATION_SOURCE_SETTINGS screen looks like the following…
In general what I’ll do is use the LocationManager class to check for the availability of the required location services. If the services aren’t enabled, I’ll show an AlertDialog informing the user of what they need to do and then show the settings screen in the dialog’s click handler. Here’s that code…
// Get Location Manager and check for GPS & Network location services LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { // Build the alert dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Location Services Not Active"); builder.setMessage("Please enable Location Services and GPS"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { // Show location settings when the user acknowledges the alert dialog Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } }); Dialog alertDialog = builder.create(); alertDialog.setCanceledOnTouchOutside(false); alertDialog.show(); }
That little bit of code can go a long way to both ensuring that your app has access to the required location services and that the user has a positive experience.
This post is adapted from Jim’s Plurasight Course…
For more information about Jim and his courses visit his Pluralsight Author Page
Hi, I’m new to Android. Could you please explain to me how to use the code?
That’s kind of a broad question Sausen. 🙂
If you need to ramp up on Android programming you might want to check out Building Your First App on the Android dev site or watch some of the Android courses or Beginner Programmer courses at Pluralsight.
How about making a code (or disabling the code), so that every time my app enables location services, the user won’t get a dialog message to accept the changes. I rooted the phone, and can enable gps/locservs remotely, but still the dialog pops up.. do u guys know where exactly it is written? (lets assume my phone is stolen, and I wanna enable loc servs, then it doesn’t do my any good if it notifies him).
Very nice, just what i need. Nice writing as well
Thank u!!
Gracias!! Muy buena información, justo lo que necesitaba.
[…] this nice alternative this: https://hedgehogjim.wordpress.com/2013/03/20/programmatically-enable-android-location-services/ […]
Very helpfull, thank you
I dont want to navigate it to settings. Location should enable automatically when clicked ok without directing to settings.
I hear ya, Niky … unfortunately that’s not how things work.
The user has ultimate control over whether such features are enabled.
There are many apps which are working such. Like ola app as an example. Which doesn’t navigate you to location settings, rather on it automatically when clicked on.
Agreed … for apps targeting Android 6.0 or above because Android 6.0 introduced the on-demand security model.
This post was written in March of 2013 .. in the Android 4.x timeframe.
Pre-Android 6.0 devices still represent nearly 50% of Android devices currently in use .. and on those devices … we have to take the user to the settings screen.
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
in this line “Settings ” get error . how to solve it ?
Well it’s hard to say without knowing the specific error message.
One thing that comes to mind is the class imports … Did you import the Settings class (android.provider.Settings)?