Check if 'Access to my location' is enabled - Android
Solution 1
may be this will be useful check this site it discusses about location service
http://www.scotthelme.co.uk/android-location-services/
Solution 2
You can check it like that:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) // Return a boolean
EDIT:
If you want to check the network provider:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) // Return a boolean
EDIT 2:
If you want to open the settings, you can use this intent:
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
Solution 3
An alternative way to do that without using Settings.Secure
and without scanning all location providers would be to do:
LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
int providersCount = locationManager.getProviders(true).size(); // Listing enabled providers only
if (providersCount == 0) {
// No location providers at all, location is off
}
Related videos on Youtube
Romain Pellerin
I am a Software Engineer at Doctolib.fr. I usually spend my time programming or having fun in a terminal. I am In love with GNU/Linux. I like writing stuff in LaTeX. I also play the guitar. Apart from computers, I am also interested in space exploration, AI and self-driving electric cars. I look forward to a future with a lot less pollution. I try to read books as often as possible, in English and French. I am also a huge fan of comics. I write blog posts quite extensively on my personal website (http://romainpellerin.eu), not only about computer science but also broader topics such as the environment, my travels or politics.
Updated on June 04, 2022Comments
-
Romain Pellerin almost 2 years
I have an android app that uses location. But I noticed that if users disable the 'Access to my location' in Settings > Location access, nothing works anymore. How can I check that it's enabled? In case it's disabled, how to open those settings from my app?
Thanks
SOLVED :
String locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (locationProviders == null || locationProviders.equals("")) { ... startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); }
-
Ashana.Jackol over 6 yearsi dont know why some one gives me -1 for this...this solution is works for me and im using it.if the solution didnt work with you try another solution dont mark as -1 idiot,