LocationManager.NETWORK_PROVIDER need network, but it is real, not precise. if you want to use LocationManager.GPS_PROVIDER, the situation must be outdoor instead of indoor, because GPS location need satellite, if you are in any building, the satellite cannot find you! pleasle go outdoor and check with GPS_PROVIDER again!

*best approach is getting location from both of GPS and NETWORK and check any of then that was not null and more accurate useing it.

Answer from Mohammad Reisi on Stack Overflow
🌐
Android Developers
developer.android.com › core areas › sensors and location › request location updates
Request location updates | Sensors and location | Android Developers
Use a boolean, requestingLocationUpdates, to track whether location updates are currently turned on. In the activity's onResume() method, check whether location updates are currently active, and activate them if not:
Top answer
1 of 3
13

The primary reason why you aren't getting updated location information quickly is that you're relying on the NETWORK_PROVIDER in the RestaurantHelper.getLastKnownLocation() method, but registering a LocationListener for the GPS_PROVIDER in onCreate().

So, this code in RestaurantHelper.getLastKnownLocation():

Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

...should be changed to:

Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

In theory, this should then give you the latest GPS location, which should have been refreshed when you register the listener. Conversely, you could also change to listening to the NETWORK_PROVIDER in onCreate() and leave RestaurantHelper.getLastKnownLocation() as is. It depends on your accuracy requirement - if you want high accuracy locations to return the nearest location to the nearest building level (e.g., 5-30m), you should use the GPS_PROVIDER. But, if you can live with coarser accuracy, the NETWORK_PROVIDER typically returns a new location much faster than GPS, especially if you're indoors, and sometimes this can be fairly accurate if derived from WiFi.

Another approach would be to listen to both GPS_PROVIDER and NETWORK_PROVIDER by registering both via two requestLocationUpdates() lines in onCreate(), and then checking to see most recent timestamp on the Location from lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); and lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);, and using the one that was updated more recently.

I would also recommend the following changes to make your code reliable on a large number of Android devices:

  1. Specify the requestLocationUpdates() minDistance parameter as 0 when listening for GPS or NETWORK location updates - the minDistance parameter has a history of being unreliable and unpredictable in the way its interpreted by OEMs, until Android 4.1.
  2. Switch to the new Fused Location Provider - this should be much more reliable when calling the getLastKnownLocation() method than the Android framework location APIs, and more consistent across different devices. Note that this relies on Google Play Services SDK, which is only available on Android 2.2 and higher.
2 of 3
5

I have 2 advice for you

  1. LocationClient video, is the new way of doing location stuff. It has improvements over the LocationManager that can be a pain to manage and develop.

  2. If you need to use LocationManager, you must know that requestLocationUpdates is buggy (very buggy). Since all its implementations on custom hardware differ. There is a hack/workaround that works. Before you call requestLocationUpdates, just kick start it with the following

Code :

 HomeScreen.getLocationManager().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) {
        }
    });
🌐
Google
developers.google.com › google play services › fusedlocationproviderclient
FusedLocationProviderClient | Google Play services | Google for Developers
October 31, 2024 - This API has the same background location limits that apply to requestLocationUpdates(LocationRequest, PendingIntent) and all location APIs, so clients may note that this API fails to return a location more often when invoked from the background.
🌐
DaniWeb
daniweb.com › programming › mobile-development › threads › 516044 › requestlocationupdates-does-not-work
android-development - requestLocationUpdates does not work [SOLVED] | DaniWeb
April 27, 2018 - I have made a simple app which gets curren location (latitude and longitude). It is working perfectly. But now i want to take location updates. For this i implemented LocationListener interface . I overridded the metod On location Change, but when i run the application, this method does not respond in any way.
🌐
Stack Overflow
stackoverflow.com › questions › 71582020 › requestlocationupdates-is-not-working-on-android-11-12
locationmanager - requestLocationUpdates is not working on Android 11 & 12 - Stack Overflow
This code worked well for Android 9 and 10, but it is not working for 11 and 12. The initial location is updated but the following location is not updated at all. FINE and COARSE location permissions are requested. What could be the problem? if (!isGPSEnabled && !isNetworkEnabled) { lat = 0; lon = 0; } else { this.isGetLocation = true; // GPS if (isGPSEnabled) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, this); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); if(location != null && location.getTime()
🌐
Unreal Engine
forums.unrealengine.com › development › platform & builds
5.3.2 packed android GPS ACCESS_FINE_LOCATION not working - #2 by MathewW - Platform & Builds - Epic Developer Community Forums
June 21, 2024 - I don’t know if you found an answer to this yet, but I just ran into the same problems and finally found a “solution” The Provider that is being used for the requestLocationUpdates in the Android portion of the plugin is defaulting now to fused which seems to not work very well by default unless you do some other changes to support it.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 15821653 › requestlocationupdates-not-working-for-service
android - requestLocationUpdates not working for Service - Stack Overflow
@Mugen, nope you can definitely call requestLocationUpdates from a background service. I would read up on service lifecycle and thread handling, my guess is your service is probably starting, and then just stopping... again back to my answer above. ... When calling this code from the Activity that is currently running, the myLocationListenerInstance is getting a notification in less than TIMEOUT seconds.
🌐
YouTube
youtube.com › programmer world
How to RequestLocationUpdates if FusedLocationProvider.LastLocation return null in Android App? - YouTube
This video shows the steps to implement request location updates APIs for enforcing the App and Android device to get the updated location information. This ...
Published   March 30, 2023
Views   2K
🌐
Stack Overflow
stackoverflow.com › questions › 45688901 › android-requestlocationupdates-not-called-properly
gps - Android requestLocationUpdates not called properly - Stack Overflow
I think onLocationChanged() is not suppose to be called always, it will only be called if a location changes. ... I changed the request method to lm.requestSingleUpdate(criteria, listener,null); which works perfect as well as requestLocationUpdates with criteria.
🌐
Stack Overflow
stackoverflow.com › questions › 25735978 › locationmanager-requestlocationupdates-not-responding
android - LocationManager.requestLocationUpdates not responding - Stack Overflow
might it be a problem because i call an intent service from another instance of intent service? although the intent service works fine ... Just making sure - is GPS turned on? do you get location updates from GPS on another app (e.g. Google Maps)? If not, that might not be an issue with your code but a device setting (or location since GPS won't work inside buildings...)
🌐
Tabnine
tabnine.com › home › code library
LocationManager.requestLocationUpdates - Java
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › android.locations.locationmanager.requestlocationupdates
LocationManager.RequestLocationUpdates Method (Android.Locations) | Microsoft Learn
Note: Since Android KitKat, Criteria requests will always result in using the #FUSED_PROVIDER. See #requestLocationUpdates(String, long, float, PendingIntent) for more detail on how this method works.
🌐
Android Developers
developer.android.com › api reference › locationmanager
LocationManager | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Coderanch
coderanch.com › t › 640701 › GPS-identified-locationmanager
GPS not identified by the locationmanager (Android forum at Coderanch)
You are using wrong code to request location updates. You need to use LocationManager.GPS_PROVIDER. Correct Code snippet for accessing location from GPS Complete code for onCreate() method of your activity. Refer complete android gps location manager tutorial. What if GPS is not available?