Skip to content Skip to sidebar Skip to footer

Locationmanager.getlastknownlocation(locationmanager.network_provider) Always Returns Null On Galaxy S7 (only)

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 2

Solution 1:

From my experience with Samsung devices. If you reboot the phone and you DON'T enable GPS you will not get a location. If you enable GPS as some point - maybe by running Google Maps, then you will get a location. Samsung does not return a location for GPS, Network or Passive providers unless it has gotten a location during the devices power on cycle.

None of the calls I have made in code ever get it to kick in and activate and get a location. They work fine on other devices. Samsung does not seem to cache this information for very long either.

Solution 2:

Well, you are calling isProviderEnabled() so at least that is covered and using the network location should be enabled. However the exact wording of the documentation is:

"If the user has enabled this provider in the Settings menu, true is returned otherwise false is returned"

So it's just about the provider being "enabled" as in not being turned off by the user.

Does the problematic phone (the S7) have a SIM card inside? It's possible that on that specific device the network based positioning requires a SIM card or internet connection.

Referring to the the documentation you could check what these return:

locationManager.getProvider(LocationManager.NETWORK_PROVIDER).requiresCell();
locationManager.getProvider(LocationManager.NETWORK_PROVIDER).requiresNetwork();

And of course just calling getLastKnownLocation() will return null if the location is unknown. And it's unknown if no application (yours or some other) has specifically requested the device to determine its location. So the suggestion to call requestLocationUpdates() is the correct advice. But it may take a while to determine the location so calling getLastKnownLocation() right after probably still returns null.

And even if it returns something, it might be very old data that's not even valid anymore. So why not just subscribe to receive location updates? That's the way it's intended. You are using network based positioning so it won't (at all/significantly) affect power consumption and the updates won't come too often if you specify time interval and distance limits that suit your needs.

Post a Comment for "Locationmanager.getlastknownlocation(locationmanager.network_provider) Always Returns Null On Galaxy S7 (only)"