android locationManager requestLocationUpdates

2019-04-17 09:58发布

问题:

Is there a way to request location updates from a locationManager at specific intervan and to ignore minDistance? I've tried

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3600 * 1000, 1, this)

But in logs appears sometimes that location is updated in a few minutes, sometimes in a half of hour... Can this be done to update location at a fixed interval and to ignore distance?

回答1:

You can use this schema. Create new Runnable which will be called every time, when you want it

private final Handler _handler = new Handler();
private static int DATA_INTERVAL =  60 * 1000;

private final Runnable getData = new Runnable()
{
    @Override
    public void run()
    {
        getDataFrame();
    }
};
private void getDataFrame() 
{
    requestGPS();
    Timer time = new Timer();
    time.schedule(new TimerTask() {
            @Override
            public void run() {
                _locationManager.removeUpdates(_connector);
                this.cancel();
            }
        }, 5000, 5000);
    _handler.postDelayed(getData, DATA_INTERVAL);
}
private void requestGPS() 
{
    _locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER,
                0,
                0,
                _connector);
}