I have tried searching for answers online but they seemed to work only for android versions 4.0 and below
Asked
Active
Viewed 8,079 times
4
-
3Please provide a [mcve] demonstrating what you tried and explain, in detail, what you mean by "they seemed to work only for android versions 4.0 and below". – CommonsWare May 17 '17 at 11:50
2 Answers
4
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 (SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}else{
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}

Mehraj Malik
- 14,872
- 15
- 58
- 85
-
1
-
This code is deprecated. can you please suggest alternative code? – Maulik Baraiya Feb 27 '19 at 06:03
1
You can use LocationManager
This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.
Here is a code snippet for what you are looking for
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
This has been tested from Android 4.4 and above versions.But Doesn't work for 8.0.

Shahkar Raza
- 355
- 3
- 11

Fabio
- 782
- 4
- 8
- 25