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
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
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 · 中文 – 简体
🌐
GitHub
gist.github.com › mizutori › 5aa229c6560d0c694e6adad5b6cf1773
locationManager.requestLocationUpdates · GitHub
locationManager.requestLocationUpdates · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
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.
🌐
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);...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › android.locations.locationmanager.requestsingleupdate
LocationManager.RequestSingleUpdate Method (Android.Locations) | Microsoft Learn
See #requestLocationUpdates(long, float, Criteria, PendingIntent) for more detail on how to use this method. This member is deprecated. Use #getCurrentLocation(String, CancellationSignal, Executor, Consumer) instead as it does not carry a risk of extreme battery drain. Java documentation for android.location.LocationManager.requestSingleUpdate(android.location.Criteria, android.location.LocationListener, android.os.Looper).
Find elsewhere
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":

class 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");
        }
    }
}
🌐
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 // ...
🌐
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);
🌐
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 3
11

It will update the existing request.

2 of 3
2

You could register it multiple times. see my code below.

package com.test.locationmanager;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class LocationManagerStatus extends Activity {

private LocationManager locationManager;
private TextView textView;
private final LocationListener gpsLocationListener =new LocationListener(){

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        final String tvTxt = textView.getText().toString();
        switch (status) {
        case LocationProvider.AVAILABLE:
            textView.setText(tvTxt + "GPS available again\n");
            break;
        case LocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "GPS out of service\n");
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt + "GPS temporarily unavailable\n");
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Enabled\n");
    }

    @Override
    public void onProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Disabled\n");
    }

    @Override
    public void onLocationChanged(Location location) {
        locationManager.removeUpdates(networkLocationListener);
        textView.setText(textView.getText().toString()
                + "New GPS location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};
private final LocationListener networkLocationListener =
                                                    new LocationListener(){

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras){
        final String tvTxt = textView.getText().toString();
        switch (status) {
        case LocationProvider.AVAILABLE:
            textView.setText(tvTxt + "Network location available again\n");
            break;
        case LocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "Network location out of service\n");
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt
                    + "Network location temporarily unavailable\n");
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Enabled\n");
    }

    @Override
    public void onProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Disabled\n");
    }

    @Override
    public void onLocationChanged(Location location) {
        textView.setText(textView.getText().toString()
                + "New network location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.textview);
    locationManager = (LocationManager) 
            getSystemService(Context.LOCATION_SERVICE);
}

@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 5000, 0,
            networkLocationListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            3000, 0, gpsLocationListener);
}

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(networkLocationListener);
    locationManager.removeUpdates(gpsLocationListener);
}
}