OnLocationChanged is not called even if GPS provider is available
I am trying to get the current location of a device using MyCustomLocationListener
.
My code goes through here and indicates that GPS and network providers are available.
// getting GPS status
isGPSEnabled = androidLocationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = androidLocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
Log.e(MyLogger.TAG, "Non providers are enabled");
showEnableGpsDialog();
return;
}
if (isGPSEnabled) {
androidLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
saveToSharedPrefListener);
Log.d(MyLogger.TAG, "GPS Enabled");
}
if (isNetworkEnabled) {
if (androidLocationManager == null) {
androidLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0,
saveToSharedPrefListener);
Log.d(MyLogger.TAG, "Network Enabled");
}
}
but then onLocationChanged
never called when i am closed.
Only when I use the app Fake GPS
is the onLocationChanged
.
How can I write a backup (with \ out timeout) to force invoke onLocationChanged
I used a different API requestSingleUpdate
:
What is the difference between the two when it comes to indoor work?
if (isGPSEnabled) {
androidLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
saveToSharedPrefListener);
Log.d(MyLogger.TAG, "GPS Enabled");
}
and
if (BaseApplication.getCurrentActivity() != null) {
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER,
saveToSharedPrefListener, BaseApplication.getCurrentActivity()
.getMainLooper());
source to share
I am assuming the network location provider is never requested for location updates, as per your zero code check, otherwise you will see an exception:
if (isNetworkEnabled) {
if (androidLocationManager == null) {
androidLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0,
saveToSharedPrefListener);
Log.d(MyLogger.TAG, "Network Enabled");
}
}
source to share
- but then onLocationChanged is never called when I am closed.
There is nothing you can do about it. But some phones can get indoor space, it is connected with the phone GPS receiver , so the problem is with the hardware.
Then, if you can figure out that you are indoors, you can use LocationManager.NETWORK_PROVIDER
to get the location. But it works with more precision than LocationManager.GPS_PROVIDER
.
-
What is the difference between the two in the workroom?
- If your GPS receiver was unable to receive location updates (sometimes inside, sometimes bad weather, also with regard to the phone hardware), it doesn't matter which unit you use.
- If your GPS receiver can receive location updates, use the former to keep your location updated.
Please note that: "GPS provider is available" does not mean that you can get a location with it. Unreasonable, but on Android devices it does.
I want this to help.
source to share