Just received a Galaxy S7 (Edge) running Marshmallow (6.0.1) and find that it has an issue with my app that uses android.permission.ACCESS_COARSE_LOCATION
and targets Sdk Version 22 (Lollipop). When I call locationManager.getLastKnownLocation()
it always returns NULL. Here's what I'm running:
public Location getLastKnownLocationObject(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try {
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// Location is always null on S7 (only)
Log.i(TAG, ">>> getLastKnownLocationObject() getLastKnownLocation: " + location);
return location;
}
} catch (Exception e) {
Log.e(TAG, ">>> getLastKnownLocationObject() exception", e);
}
return null;
}
This same code works fine on every other device I've tried: Galaxy S5 (5.0.1), Nexus 7 (5.0.1), Nexus 9 (6.0.1), and Samsung Tab3 (4.4.2)
Note that if I change the manifest to use ACCESS_FINE_LOCATION, the above code works fine on every device. Unfortunately, management won't allow me to change permissions at this time.
In a few answers I've seen here on SO, it's suggested to call the following prior to doing the getLastKnownLocation(), but that didn't have any effect.
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
// NEVER CALLED on Samsung S7 for LocationManager.NETWORK_PROVIDER
}
});
So at the moment, I feel this is an issue exclusive to the new Samsung Galaxy S7.