You are implementing LocationListener in your activity MainActivity. The call for concurrent location updates will therefor be like this:

mLocationClient.requestLocationUpdates(mLocationRequest, this);

Be sure that the LocationListener you're implementing is from the google api, that is import this:

import com.google.android.gms.location.LocationListener;

and not this:

import android.location.LocationListener;

and it should work just fine.

It's also important that the LocationClient really is connected before you do this. I suggest you don't call it in the onCreate or onStart methods, but in onResume. It is all explained quite well in the tutorial for Google Location Api: https://developer.android.com/training/location/index.html

Answer from HigiPha on Stack Overflow
🌐
Android Developers
developer.android.com › core areas › sensors and location › request location updates
Request location updates | Sensors and location | Android Developers
override fun onResume() { super.onResume() if (requestingLocationUpdates) startLocationUpdates() } private fun startLocationUpdates() { fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper()) }
Top answer
1 of 2
40

You are implementing LocationListener in your activity MainActivity. The call for concurrent location updates will therefor be like this:

mLocationClient.requestLocationUpdates(mLocationRequest, this);

Be sure that the LocationListener you're implementing is from the google api, that is import this:

import com.google.android.gms.location.LocationListener;

and not this:

import android.location.LocationListener;

and it should work just fine.

It's also important that the LocationClient really is connected before you do this. I suggest you don't call it in the onCreate or onStart methods, but in onResume. It is all explained quite well in the tutorial for Google Location Api: https://developer.android.com/training/location/index.html

2 of 2
13

I use this one:

LocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)

For example, using a 1s interval:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);

the time is in milliseconds, the distance is in meters.

This automatically calls:

public void onLocationChanged(Location location) {
    //Code here, location.getAccuracy(), location.getLongitude() etc...
}

I also had these included in the script but didnt actually use them:

public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}

In short:

public class GPSClass implements LocationListener {

    public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude());
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
    }
}
🌐
Google
developers.google.com › google play services › fusedlocationproviderclient
FusedLocationProviderClient | Google Play services | Google for Developers
October 31, 2024 - On the other hand, if repeated location updates are required, such as when tracking the user's location over time, requestLocationUpdates(LocationRequest, Executor, LocationListener) or one of its variants is better suited. Clients are encourage to familiarize themselves with the full range of APIs available in this class to understand which is best suited for their needs. This constant is deprecated. Use Location.isMock() on Android S and above, otherwise use LocationCompat.isMock() from the compat libraries instead.
🌐
Java2s
java2s.com › example › java-api › android › location › locationmanager › requestlocationupdates-4-3.html
Example usage for android.location LocationManager requestLocationUpdates
String provider = locationManager.getBestProvider(criteria, true); //provider?????????????? if (provider != null) { Toast.makeText(this, provider + "???", Toast.LENGTH_LONG).show(); Log.d("app", "the best provider is " + provider); // ??? Location location = locationManager.getLastKnownLocation(provider); if (location != null) { onLocationChanged(location); } locationManager.requestLocationUpdates(provider, 20000, 0, this); } else { //GPS????????? Toast.makeText(this, "????????", Toast.LENGTH_LONG).show(); } } } From source file:it.geosolutions.geocollect.android.core.mission.PendingMissionListActivity.java
🌐
Program Creek
programcreek.com › java-api-examples
android.location.LocationManager#requestLocationUpdates
The following examples show how to use android.location.LocationManager#requestLocationUpdates() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar. Example 1 · /** * Initialize starting values and starting best location listeners * * @param ctx * @param result */ public void init(Context ctx, LocationResult result) { context = ctx; locationResult = result; myLocationManager = (LocationManager) context.getSystemService(Context.L
🌐
Android Developers
minimum-viable-product.github.io › kitkat-docs › training › location › receive-location-updates.html
Receiving Location Updates | Android Developers
At this point, you can * request the current location or start periodic updates */ @Override public void onConnected(Bundle dataBundle) { // Display the connection status Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); // If already requested, start periodic updates if (mUpdatesRequested) { mLocationClient.requestLocationUpdates(mLocationRequest, this); } } ... } For more information about saving preferences, read Saving Key-Value Sets. To stop location updates, save the state of the update flag in onPause(), and stop updates in onStop() by calling removeLocationUpdates(LocationListener). For example:
🌐
GitHub
github.com › mkett › android-location-listener-example
GitHub - mkett/android-location-listener-example: Example to request location changes on Android · GitHub
private val locationListener = ... fun registerToLocationListener() { ... locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 9000, 0f, locationListener ) }...
Author   mkett
Find elsewhere
🌐
GitHub
github.com › djshah17 › Location-Updates-Service-Sample
GitHub - djshah17/Location-Updates-Service-Sample
{ val binder: LocationUpdatesService.LocalBinder = service as LocationUpdatesService.LocalBinder mService = binder.service mBound = true // Check that the user hasn't revoked permissions by going to Settings. mService?.requestLocationUpdates() ...
Starred by 6 users
Forked by 7 users
Languages   Kotlin 100.0% | Kotlin 100.0%
🌐
Google
developers.google.com › google play services › locationrequest
LocationRequest | Google Play services | Google for Developers
October 31, 2024 - For example, if a request is made with a 2s interval and a 10s maximum update delay, this implies that the device may choose to deliver batches of 5 locations every 10s (where each location should represent a point in time ~2s after the previous).
🌐
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 · 中文 – 简体
🌐
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
[<Android.Runtime.Register("requestLocationUpdates", "(Ljava/lang/String;Landroid/location/LocationRequest;Landroid/app/PendingIntent;)V", "GetRequestLocationUpdates_Ljava_lang_String_Landroid_location_LocationRequest_Landroid_app_PendingIntent_Handler", ApiSince=31)>] abstract member RequestLocationUpdates : string * Android.Locations.LocationRequest * Android.App.PendingIntent -> unit override this.RequestLocationUpdates : string * Android.Locations.LocationRequest * Android.App.PendingIntent -> unit
🌐
Finotes Blog
blog.finotes.com › post › efficient-ways-of-using-location-services-in-kotlin-android-apps
Efficient Ways of Using Location Services in Kotlin Android Apps
July 4, 2023 - Here's an example: private val locationRequest: LocationRequest = LocationRequest.create().apply { interval = 10000 // Update interval in milliseconds fastestInterval = 5000 // Fastest update interval in milliseconds priority = LocationRequest.PRIORITY_HIGH_ACCURACY } private fun startLocationUpdates() { fusedLocationClient.requestLocationUpdates( locationRequest, locationCallback, Looper.getMainLooper() ) } private val locationCallback = object : LocationCallback() { override fun onLocationResult(locationResult: LocationResult?)
🌐
Milosev
milosev.com › home › android
Request location updates - Android - Milosev
November 30, 2025 - locationCallback = object : LocationCallback() { override fun onLocationResult(locationResult: LocationResult) { locationResult ?: return if (locationResult.locations.isNotEmpty()) { // get latest location val location = locationResult.lastLocation if (location != null) { println(location.latitude.toString()) println(location.longitude.toString()) } } } } In app\src\main\AndroidManifest.xml I have added permissions:
🌐
Android Developers
stuff.mit.edu › afs › sipb › project › android › docs › guide › topics › location › strategies.html
Location Strategies | Android Developers
You can also request location updates from both the GPS and the Network Location Provider by calling requestLocationUpdates() twice—once for NETWORK_PROVIDER and once for GPS_PROVIDER. In order to receive location updates from NETWORK_PROVIDER or GPS_PROVIDER, you must request user permission by declaring either the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission, respectively, in your Android manifest file. For example:
🌐
Android Clarified
androidclarified.wordpress.com › 2018 › 03 › 13 › android-example-request-location-updates-with-fusedlocationproviderapi
Android Example : Request Location Updates with FusedLocationProviderApi – Android Clarified
March 13, 2018 - In our last post we developed a app to retrieve the current location of the device on Android. While retrieving the current location can be useful in a lot of real life scenarios sometimes it is just not enough. Thanks to Google’s FusedLocationProviderApi receiving location change updates in the app is fairly simple. In the following example we will be implementing an app which will be receiving updates on every location change.
🌐
Stack Overflow
stackoverflow.com › questions › 23159117 › android-requestlocationupdates
Android: requestLocationUpdates - Stack Overflow
I have managed to get a fix on an android device's location (both with network provider and gps provider) using: locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); locationManager.requestLocati...
Top answer
1 of 3
2

Firstly add this implementation in your build.gradle file

implementation 'com.google.android.gms:play-services-location:11.2.0'//include the latest version of play services

After that implement, (implements LocationListener) in your activity or fragment and after that implement its function and then

call this method in your onCreate() getLocation(); and then in this function add these lines

protected void getLocation() {
    if (isLocationEnabled(MainActivity.this)) {
        locationManager = (LocationManager)  this.getSystemService(Context.LOCATION_SERVICE);
        criteria = new Criteria();
        bestProvider = String.valueOf(locationManager.getBestProvider(criteria, true)).toString();

        //You can still do this if you like, you might get lucky:
        Location location = locationManager.getLastKnownLocation(bestProvider);
        if (location != null) {
            Log.e("TAG", "GPS is on");
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            Toast.makeText(MainActivity.this, "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();
            searchNearestPlace(voice2text);
        }
        else{
            //This is what you need:
            locationManager.requestLocationUpdates(bestProvider, 1000, 0, this);
        }
    }
    else
    {
        //prompt user to enable location....
        //.................
    }
}

After that in your onLocationChanged(Location location) add these lines of code

@Override
public void onLocationChanged(Location location) {
    //Hey, a non null location! Sweet!

    //remove location callback:
    locationManager.removeUpdates(this);

    //open the map:
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    Toast.makeText(MainActivity.this, "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();
    searchNearestPlace(voice2text);
}

and you are set to go!!!! Cheers Happy Coding

2 of 3
2

It's work for me. Based on Official docs, every 10 seconds updates the Toast and shows your location:

private FusedLocationProviderClient fusedLocationClient;
private LocationRequest locationRequest;
private LocationCallback locationCallback;



@Override
protected void onResume() {
    super.onResume();
    startLocationUpdates();

}

private void startLocationUpdates() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    Activity#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for Activity#requestPermissions for more details.
            return;
        }
    }
    fusedLocationClient.requestLocationUpdates(locationRequest,
            locationCallback,
            Looper.getMainLooper());
}

@Override
protected void onPause() {
    super.onPause();
    stopLocationUpdates();
}

private void stopLocationUpdates() {
    fusedLocationClient.removeLocationUpdates(locationCallback);
}

add bellow codes to onCreate method:

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(2*5000);

    locationCallback=new LocationCallback(){
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            for (Location location : locationResult.getLocations()) {
               // if (location != null) {


                    String Lat = String.valueOf(location.getLatitude());
                    String Lon = String.valueOf(location.getLongitude());

                    Toast.makeText(getApplicationContext(), Lat + " - " + Lon, Toast.LENGTH_SHORT).show();
            //    }
            }
        }
    };