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
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › android.locations.locationmanager.requestlocationupdates
LocationManager.RequestLocationUpdates Method (Android.Locations) | Microsoft Learn
Register for location updates from the specified provider, using a LocationRequest, and callbacks delivered via the provided PendingIntent.
🌐
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 · 中文 – 简体
🌐
Android Developers
developer.android.com › core areas › sensors and location › request location updates
Request location updates | Sensors and location | Android Developers
This document explains how to request regular updates about a device's location using the Fused Location Provider's requestLocationUpdates() method in Android.
🌐
PCC
spot.pcc.edu › ~mgoodman › developer.android.com › guide › topics › location › strategies.html
Location Strategies | Android Developers
As demonstrated above, you can ... GPS location data: // String locationProvider = LocationManager.GPS_PROVIDER; locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);...
🌐
Java2s
java2s.com › example › java-api › android › location › locationmanager › requestlocationupdates-4-3.html
Example usage for android.location LocationManager requestLocationUpdates
// See http://developer.android.com/reference/android/location/LocationManager.html locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 180000, 3, new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } // When the location changes, store the current location in the parent activity @Override public void onLocationChanged(Location location) { currentLocation = location; } }); // Attach the "lookup locat
🌐
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.
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);
    }
}
🌐
Android Developers
stuff.mit.edu › afs › sipb › project › android › docs › reference › android › location › LocationManager.html
LocationManager - Android SDK | Android Developers
The requestLocationUpdates() and requestSingleUpdate() register the current activity to be updated periodically by the named provider, or by the provider matching the specified Criteria, with location and status updates.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 13442991 › locationmanager-requestlocationupdates
android - LocationManager requestLocationUpdates - Stack Overflow
Handler handler; // this Handler is initialized in the following thread Runnable r = new Runnable() { public void run() { Looper.prepare(); handler = new Handler() { @Override public void handleMessage(Message msg) { Log.d("MSG", msg.toString()); } }; Looper.loop(); } }; Thread t = new Thread(r); t.start(); LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() { public void onLocationChanged(Location location) { Log.d("UPD", "onLocationChanged"); } [...] }, handler.getLooper());
Top answer
1 of 1
1

Workaround for this issue is to use only one LocationManager and one LocationListener. If your app has needs for different kind of simultaneous location requests (with different parameters), then you need to implement a "location request handler" which decides which parameters should be used for the location request i.e. which parameters have the tightest requirements for location.

Here is a simple example code that explains the idea of "location request handler":

Copyclass LR {

    long lock_min_time; // defined in set_lock_lr before using
    float lock_min_dist;
    boolean lock_active = false;

    long idle_min_time = 3600000; // 1 per hour
    float idle_min_dist = 200;
    boolean idle_active = true;

    long fast_min_time = 0;
    float fast_min_dist = 0;
    boolean fast_active = false;

    //constructor
    public LR()
    {}

    public void set_lock_lr(long min_time, float min_dist, boolean active)
    {
        lock_active = active;
        lock_min_dist = min_dist;
        lock_min_time = min_time;
        System.out.println("LR lock set: "+min_time+", "+min_dist+", "+active);
        update_location_request();
    }

    public void set_idle_lr(boolean active)
    {
        idle_active = active;
        System.out.println("LR idle set: "+active);
        update_location_request();
    }

    public void set_fast_lr(boolean active)
    {
        fast_active = active;
        System.out.println("LR fast set: "+active);
        update_location_request();
    }

    private void update_location_request()
    {
        // Remove current location request
        mlocManager_basic.removeUpdates(mlocListener_basic);

        if(fast_active)
        {
            mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, fast_min_time, fast_min_dist, mlocListener_basic);
            System.out.println("LR: fast_active");
        }
        else if(lock_active)
        {
            mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, lock_min_time, lock_min_dist, mlocListener_basic);
            System.out.println("LR: lock_active");
        }
        else if(idle_active) // only idle updates
        {
            mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, idle_min_time, idle_min_dist, mlocListener_basic);
            System.out.println("LR: idle_active");
        }
    }
}
🌐
Stack Overflow
stackoverflow.com › questions › 22901427 › locationmanager-updates
android - locationManager updates - Stack Overflow
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, Integer.MAX_VALUE, 1 locationListener); From http://developer.android.com/guide/topics/location/strategies.html
🌐
Medium
sachankapil.medium.com › latest-method-how-to-get-current-location-latitude-and-longitude-in-android-give-support-for-c5132474c864
Latest Method: How to get Current Location (Latitude and Longitude) in Android & Give support for…
January 27, 2023 - if (hasGps) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 0F, gpsLocationListener ) }//------------------------------------------------------//if (hasNetwork) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 5000, 0F, networkLocationListener ) }
🌐
Medium
medium.com › @psarakisnick › android-location-manager-with-kotlin-flows-082c992d1b31
Android location manager with Kotlin flows | by Nick Psarakis | Medium
January 21, 2024 - client.requestLocationUpdates(request, locationCallback, Looper.getMainLooper()) The Looper.getMainLooper() part is so we keep running this request running based on the parameters we passed on the request object. Now that we setup and run the request. after we should close the flow and the request if no one is listening anymore. We can do that with · awaitClose { client.removeLocationUpdates(locationCallback) } ... class LocationManagerImpl( private val context: Context ) : LocationManager { private val client: FusedLocationProviderClient by lazy { LocationServices.getFusedLocationProviderCli
🌐
Medium
medium.com › @boobalaninfo › accessing-users-location-guide-android-2023-60a6f018a718
Accessing User’s Location Guide Android 2023 | by Boobalan Munusamy | Medium
June 22, 2023 - // Create a LocationManager instance val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager // Request location updates from GPS provider locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, locationListener) // Define a LocationListener to handle location updates val locationListener = object : LocationListener { override fun onLocationChanged(location: Location) { // Handle received location updates val latitude = location.latitude val longitude = location.longitude // ...
🌐
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 = object : LocationListener { override fun onLocationChanged(location: Location) { ... } } private fun registerToLocationListener() { ... locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 9000, 0f, locationListener ) }
Author   mkett