android locationManager requestLocationUpdates

若如初见. 提交于 2020-01-13 07:23:30

问题


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);
}


来源:https://stackoverflow.com/questions/5947775/android-locationmanager-requestlocationupdates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!