How to check if Location Services are enabled?
You can use the below code to check whether gps provider and network providers are enabled or not.
LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {}
if(!gps_enabled && !network_enabled) {
// notify user
new AlertDialog.Builder(context)
.setMessage(R.string.gps_network_not_enabled)
.setPositiveButton(R.string.open_location_settings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton(R.string.Cancel,null)
.show();
}
And in the manifest file, you will need to add the following permissions
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
In flutter how i can check if location is enabled?
You can install the permission handler plugin and then use it:
final PermissionStatus permission = await PermissionHandler()
.checkPermissionStatus(PermissionGroup.location);
how to check if Location services is enabled in the device under Google API
The following code check if location is enabled or not. If not enabled it shows alert dialog.
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch (Exception ex){}
try{
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}catch (Exception ex){}
if(!gps_enabled && !network_enabled){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage(getResources().getString(R.string.gps_network_not_enabled));
dialog.setPositiveButton(getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
Startup.this.startActivity(myIntent);
}
});
dialog.setNegativeButton(getString(R.string.Cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
Check if location services are enabled
Add the CLLocationManagerDelegate
to your class inheritance and then you can make this check:
Import CoreLocation Framework
import CoreLocation
Swift 1.x - 2.x version:
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .NotDetermined, .Restricted, .Denied:
print("No access")
case .AuthorizedAlways, .AuthorizedWhenInUse:
print("Access")
}
} else {
print("Location services are not enabled")
}
Swift 4.x version:
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
}
} else {
print("Location services are not enabled")
}
Swift 5.1 version
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
@unknown default:
break
}
} else {
print("Location services are not enabled")
}
iOS 14.x
In iOS 14 you will get the following error message:authorizationStatus() was deprecated in iOS 14.0
To solve this, use the following:
private let locationManager = CLLocationManager()
if CLLocationManager.locationServicesEnabled() {
switch locationManager.authorizationStatus {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
@unknown default:
break
}
} else {
print("Location services are not enabled")
}
How to know in android if location is turned off by user
public static boolean canGetLocation() {
return isLocationEnabled(App.appInstance); // application context
}
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
return !TextUtils.isEmpty(locationProviders);
}
}
This is a quick tool to check if device location is enabled or not, Hope this helps.
This will return a boolean indicating enabled or not.
And you can navigate not location settings by using the following intent from the alert dialog click listener,
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
Android: Check if Location Services Enabled using Fused Location Provider
See SettingsApi: check your location request then ensure that the device's system settings are properly configured for the app's location needs.
How to check if the location service is enabled or not in Appcelerator
First of all you need to check for Location Permissions for app in Android & then you need to check if location service is enabled in device or not.
Both are different statements.
First one checks for app permission to access location & 2nd is about checking location service is on or off.
Without checking Location Permissions first on Android, you cannot check for location on/off state, else it will always lead to false status.
First of all add this in tiapp.xml in ios -> plist -> dict
<key>NSLocationAlwaysUsageDescription</key>
<string>Determine Current Location</string>
Now here's the cross-compatible code for Android/iOS.
function checkLocationEnabledOrNot(_callback, _args) {
if (Titanium.Geolocation.locationServicesEnabled) {
_callback(_args);
} else {
alert("Turn on location on your device.");
}
}
// pass _callback method you want to call after successful access to location
// you can also pass arguments as 2nd parameter to the function you want to call
function startLocationProcess(_callback, _args) {
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_HIGH;
if (OS_IOS) {
checkLocationEnabledOrNot(_callback, _args);
} else if (OS_ANDROID) {
if (Ti.Geolocation.hasLocationPermissions()) {
checkLocationEnabledOrNot(_callback, _args);
} else {
Ti.Geolocation.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS, function (locationEvent) {
if (locationEvent.success) {
checkLocationEnabledOrNot(_callback, _args);
} else {
alert("Location permissions are required to access locations.");
}
});
}
}
}
Now, on a button click whatever you want to do after location check, you can simply do it like this:
function anotherFunction(name) {
alert(name);
}
$.someButton.addEventListener('click', function (e) {
startLocationProcess(anotherFunction, "Hello D.Ish");
});
Check if 'Access to my location' is enabled - Android
may be this will be useful
check this site it discusses about location service
http://www.scotthelme.co.uk/android-location-services/
Related Topics
How to Return a Result (Startactivityforresult) from a Tabhost Activity
How to Change the Text on the Action Bar
How to Open the "Front Camera" on the Android Platform
Open Application After Clicking on Notification
Integrate Mupdf Reader in an App
How to Get the Resource Id of an Image If I Know Its Name
How to Change Datepicker Dialog Color for Android 5.0
Set Transparent Background of an Imageview on Android
Onitemclicklistener Not Working in Listview
How to Save Recyclerview'S Scroll Position Using Recyclerview.State
How to Sign Android App With System Signature
How to Handle Dynamic Json in Retrofit
How to Layout Text to Flow Around an Image
How to Solve Java.Lang.Outofmemoryerror Trouble in Android