Moises Jimenez
Moises Jimenez

Reputation: 1962

How can I get gps location updates at an specific rate?

I'm developing an application that needs to get location updates fast, doesn't matter the accuracy of them. I need to be able to get about one reading per second. How can I do this?

Upvotes: 0

Views: 1211

Answers (2)

nhaarman
nhaarman

Reputation: 100468

You could build a layer between the Android location updates and your receiver.
In your own layer, catch the Android location updates, and pass the same location 30 times per second to your receiver, until you get a new location.

EDIT
Something like this (not tested):

public class MyLocationManager implements LocationListener{

    private List<MyLocationListener> listeners;
    private Location lastLocation;
    private Handler handler;

    public MyLocationManager(){
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

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

        listeners = new ArrayList<MyLocationListener>();
        handler = new Handler();

        sendLocationUpdates();
    }

    private void sendDelayedLocationUpdates(){
        handler.postDelayed(locationUpdater, 200);
    }

    public void addMyLocationListener(MyLocationListener mListener){
        listeners.add(mListener);
    }

    public void removeMyLocationListener(MyLocationListener mListener){
        listeners.remove(mListener);
    }

    @Override
    public void onLocationChanged(Location location) {
        lastLocation = location;
    }

    public interface MyLocationListener{
        public void onLocationChanged(Location location);
    }

    private Runnable locationUpdater = new Runnable(){
            @Override
            public void run(){
                for(MyLocationListener mListener : listeners){
                    mListener.onLocationChanged(lastLocation);
                }

                sendDelayedLocationUpdates();
            }
    };
}

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1007584

Other than specifying 0 for both the minimum distance and minimum time values in requestLocationUpdates(), you have no control over the rate. Android will give you all of the fixes it receives, but whether that is 30 fixes per second or 30 seconds per fix will depend upon hardware, environment (e.g., is the user indoors?), and so forth.

Upvotes: 4

Related Questions