Reputation: 199
LocationManager
is always returning false in oreo
and above devices when i check if gps is on or not, even when i turn it on, it still returns false.
How do i check if gps is on in oreo
and and above devices?
Heres my code:
Location manager;
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//this if check is always false even when gps is on
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
void onRestart(){
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//still returning false when i come back to app from settings screen after turning on gps, but it returns true in android Nougat devices perfectly
}
}
Upvotes: 0
Views: 1706
Reputation: 3157
There is another possibility that is not really related to the device being Oreo or not, but in the settings activity you redirect via startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
you may not enabled the GPS provider itself. Also since you said Oreo and above I think you have already given the permission of android.manifest.Permission.ACCESS_FINE_LOCATION
both in runtime and in manifest.
If you noticed, there are 3 providers: GPS_PROVIDER
, NETWORK_PROVIDER
and PASSIVE_PROVIDER
. You are using GPS_PROVIDER
to fetch your location, but if it is not enabled via the settings, it might return false. Only network provider might have been selected while you're trying this.
Here is a picture of what I'm talking about (from HUAWEI-P20 Lite - Android 8.0.0 API 26):
Upvotes: 0
Reputation: 1116
Please use the fused location client
Documentation
It's battery efficient and very easy to work with and is pretty much the standard in location based apps.
A youtube tutorial
An example from one of my apps:
@Override
public void onStart() {
super.onStart();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (googleMap == null) return;// GUARD
map = googleMap;
getDeviceLocationUpdates();
}
/**
* Start listening to location changes
*/
private void getDeviceLocationUpdates() {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationRequest = new LocationRequest();
locationRequest.setInterval(Constants.DRIVING_TIME_INTERVAL);
locationRequest.setFastestInterval(Constants.DRIVING_TIME_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
List<Location> locationList = locationResult.getLocations();
if (locationList.size() > 0) {
//The last location in the list is the newest
Location location = locationList.get(locationList.size() - 1);
Log.i(TAG, "Location: " + location.getLatitude() + " " + location.getLongitude());
lastLocation = location;
if (currentLocationMarker != null) {
currentLocationMarker.remove();
}
if (mapPin == null) {
mapPin = GraphicUtils.resizeImage(getActivity(), Constants.MAP_MARKER_IMAGE_NAME, 100, 100);
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
placeCurrentLocationMarker(latLng);
//move map camera
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, Constants.DEFAULT_ZOOM));
}
}
};
/**
* Places a marker with user image on the map on given location
*
* @param location GPS Location
*/
private void placeCurrentLocationMarker(LatLng location) {
Bitmap bitmap = GraphicUtils.createContactBitmap(getActivity(), BitmapFactory.decodeResource(
getActivity().getResources(),
Integer.valueOf(((MainActivity) getActivity()).currentUser.getImgUrl())));
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(location);
markerOptions.title(getString(R.string.you_are_here));
markerOptions.icon((bitmap == null) ?
BitmapDescriptorFactory.fromBitmap(mapPin) :
BitmapDescriptorFactory.fromBitmap(bitmap));
currentLocationMarker = map.addMarker(markerOptions);
}
Upvotes: 1