那是多久以前的最后一个已知位置记录? [英] How long ago was the last known location recorded?
问题描述
我收到我的最后一个已知的位置,但没有多久,就已经因为我的位置是最后一次更新。我如何才能知道多久它已经因为工作地点是去年更新?
LocationManager locationManager
=(LocationManager)getSystemService(LOCATION_SERVICE);
标准C =新标准();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setAccuracy(Criteria.ACCURACY_COARSE);
c.setAltitudeRequired(假);
c.setBearingRequired(假);
c.setCostAllowed(真正的);
c.setPowerRequirement(Criteria.POWER_HIGH);
字符串商= locationManager.getBestProvider(C,真正的);
位置位置= locationManager.getLastKnownLocation(供应商);
为最佳的选择既pre和API后17:
公众诠释age_minutes(位置最后){
返回age_ms(最后一个)/(60 * 1000);
}
众长age_ms(位置最后){
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.JELLY_BEAN_MR1)
返回age_ms_api_17(最后一个);
返回age_ms_api_ pre_17(最后一个);
}
@TargetApi(Build.VERSION_ codeS.JELLY_BEAN_MR1)
专用长age_ms_api_17(位置最后){
返回(SystemClock.elapsedRealtimeNanos() - 最后
.getElapsedRealtimeNanos())/ 1000000;
}
专用长age_ms_api_ pre_17(位置最后){
返回的System.currentTimeMillis() - last.getTime();
}
在pre 17不是很准确,但应该足以测试一个位置是很老了。
这,我想,将是美好的:
如果(age_minutes(lastLoc)小于5){
//解决办法是在5分钟的时候,我们就用它
} 其他 {
//超过5分钟,我们会忽略它并等待新的
}
通常的用例这样的逻辑是,当应用程序刚刚开始,我们需要知道我们是否必须等待一个新的位置,或者可以使用最新的,现在在我们等待新的位置。
I am getting my last known location but not how long it has been since my location was last updated. How can I find out how long it has been since the location was last updated?
LocationManager locationManager
= (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setAccuracy(Criteria.ACCURACY_COARSE);
c.setAltitudeRequired(false);
c.setBearingRequired(false);
c.setCostAllowed(true);
c.setPowerRequirement(Criteria.POWER_HIGH);
String provider = locationManager.getBestProvider(c, true);
Location location = locationManager.getLastKnownLocation(provider);
Best option for both pre and post API 17:
public int age_minutes(Location last) {
return age_ms(last) / (60*1000);
}
public long age_ms(Location last) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
return age_ms_api_17(last);
return age_ms_api_pre_17(last);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private long age_ms_api_17(Location last) {
return (SystemClock.elapsedRealtimeNanos() - last
.getElapsedRealtimeNanos()) / 1000000;
}
private long age_ms_api_pre_17(Location last) {
return System.currentTimeMillis() - last.getTime();
}
The pre 17 is not very accurate, but should be sufficient to test if a location is very old.
This, I should think, would be OK:
if (age_minutes(lastLoc) < 5) {
// fix is under 5 mins old, we'll use it
} else {
// older than 5 mins, we'll ignore it and wait for new one
}
The usual use case for this logic is when the app has just started and we need to know whether we must wait for a new location or can use the latest for now while we wait for a new location.
这篇关于那是多久以前的最后一个已知位置记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!