Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if 'Access to my location' is enabled - Android

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));
}
like image 463
Romain Pellerin Avatar asked Aug 09 '13 15:08

Romain Pellerin


People also ask

How do I know if Android location is enabled?

Settings > Location > Mode > Battery Saving . This mode only uses WiFi, Bluetooth or mobile data instead of GPS to determine the user location. That's why you have to check if the network provider is enabled and locationManager. isProviderEnabled(LocationManager.

How do you check GPS is enable or not?

Add a TextView and a Button in the layout file. The TextView will display if the GPS is enabled or disabled upon the Button trigger.

What app is currently using my location android?

You can find out which apps actually use location tracking and just disable it for those that you feel don't need it. Go to the Location page (by long-pressing the Location icon in your Quick Settings tray). Tap on “App permission” (or, if you're using Android 12, look for “App location permissions”).

What are the required permission for Android location Manager?

Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION . The permission you choose determines the accuracy of the location returned by the API. android.


1 Answers

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);
like image 84
Brtle Avatar answered Oct 10 '22 01:10

Brtle