Mikhail Krutov
Mikhail Krutov

Reputation: 702

What is a correct way of determining elapsed time since last GPS Location update in pre-4.2 Androids?

What is a correct way to determine time since last location update on <4.2 Androids? This is code I've used:

long lastUpdateTime = lastLocation.getTime(); 
long currentTime = System.currentTimeMillis();

if (( currentTime - lastUpdateTime > 300000) ||  currentTime - lastUpdateTime < 0){
    LocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,400,1,listener);
}

Is it OK or should I have some other approach?

API Docs state this:

Note that the UTC time on a device is not monotonic: it can jump forwards or backwards unpredictably. So always use getElapsedRealtimeNanos() when calculating time deltas.

However, pre-APIv17 (where getElapsedRealtimeNanos() was added) is still around & needs to be supported.

Thanks for the answers,

Upvotes: 3

Views: 1041

Answers (1)

Boo
Boo

Reputation: 377

I'd leave this as a comment but I don't have that privilege yet. It seems a similar question has been asked already.

"How long ago was the last known location recorded?"

Upvotes: 1

Related Questions