Only update location at most every 6 minutes.
This commit is contained in:
parent
75a73d97fd
commit
4734104bd2
|
|
@ -24,6 +24,7 @@ import com.pengrad.telegrambot.TelegramBot;
|
||||||
import com.pengrad.telegrambot.model.Chat;
|
import com.pengrad.telegrambot.model.Chat;
|
||||||
import com.pengrad.telegrambot.request.SendLocation;
|
import com.pengrad.telegrambot.request.SendLocation;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class LocationLoggingService extends Service implements LocationListener {
|
public class LocationLoggingService extends Service implements LocationListener {
|
||||||
|
|
@ -95,10 +96,29 @@ public class LocationLoggingService extends Service implements LocationListener
|
||||||
|
|
||||||
private void updateLocation(Location newLocation){
|
private void updateLocation(Location newLocation){
|
||||||
|
|
||||||
if(lastLocation != null && (newLocation.distanceTo(lastLocation) < 2000))
|
if(lastLocation != null)
|
||||||
{
|
{
|
||||||
Log.d(TAG, "Distance to last location too short. Ignoring.");
|
if(newLocation.distanceTo(lastLocation) < 2000) {
|
||||||
return;
|
Log.d(TAG, "Distance to last location too short. Ignoring.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
long oldTime = lastLocation.getTime();
|
||||||
|
long newTime = newLocation.getTime();
|
||||||
|
Duration durationBetweenUpdates = Duration.ofMinutes(6);
|
||||||
|
|
||||||
|
if(oldTime > newTime)
|
||||||
|
{
|
||||||
|
Log.w(TAG, "Last location time was somehow after new location's.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
long millisSinceLastUpdate = newTime - oldTime;
|
||||||
|
if(durationBetweenUpdates.toMillis() > millisSinceLastUpdate)
|
||||||
|
{
|
||||||
|
Log.d(TAG, "Location update is too recent.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!newLocation.hasAccuracy())
|
if(!newLocation.hasAccuracy())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue