【发布时间】:2011-08-10 20:06:47
【问题描述】:
我正在寻找一种在我的应用中显示 GPS 时间的方法。 最简单的方法是什么?
(由于gps时间和内部系统时间之间存在细微的时间差异,因此必须是GPS时间)
谢谢
【问题讨论】:
我正在寻找一种在我的应用中显示 GPS 时间的方法。 最简单的方法是什么?
(由于gps时间和内部系统时间之间存在细微的时间差异,因此必须是GPS时间)
谢谢
【问题讨论】:
获取 GPS 时间可能相当混乱!为了在接受的答案中扩展讨论,onLocationChanged() 回调中的 getTime() 会根据位置的不同给出不同的答案 (不一定是 GPS)信息被检索,(基于 Nexus 5 测试):
(a) 如果使用 Google FusedLocationProviderApi(Google 位置服务 API),则 getProvider() 将返回“fused”,而 getTime() 将返回设备时间(System. currentTimeMillis())
(b) 如果使用 Android LocationManager(Android 位置 API),则取决于手机的“位置”设置和 requestLocationUpdates 设置(LocationManager.NETWORK_PROVIDER 和/或LocationManager.GPS_PROVIDER), getProvider() 将返回:
本质上:“fused”使用 GPS 和 Wi-Fi/网络,“network”使用 Wi-Fi/网络,“gps”使用 GPS。
因此,要获取 GPS 时间,请使用 Android LocationManager 并将 requestLocationUpdates 设置为 LocationManager.GPS_PROVIDER。 (注意在这种情况下 getTime() 毫秒部分始终为 000)
这是一个使用 Android LocationManager (Android Location API) 的示例:
public void InitialiseLocationListener(android.content.Context context) {
android.location.LocationManager locationManager = (android.location.LocationManager)
context.getSystemService(android.content.Context.LOCATION_SERVICE);
android.location.LocationListener locationListener = new android.location.LocationListener() {
public void onLocationChanged(android.location.Location location) {
String time = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS").format(location.getTime());
if( location.getProvider().equals(android.location.LocationManager.GPS_PROVIDER))
android.util.Log.d("Location", "Time GPS: " + time); // This is what we want!
else
android.util.Log.d("Location", "Time Device (" + location.getProvider() + "): " + time);
}
public void onStatusChanged(String provider, int status, android.os.Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
if (android.support.v4.content.ContextCompat.checkSelfPermission(context,
android.Manifest.permission.ACCESS_FINE_LOCATION) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
android.util.Log.d("Location", "Incorrect 'uses-permission', requires 'ACCESS_FINE_LOCATION'");
return;
}
locationManager.requestLocationUpdates(android.location.LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
locationManager.requestLocationUpdates(android.location.LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
// Note: To Stop listening use: locationManager.removeUpdates(locationListener)
}
【讨论】:
Return the UTC time of this fix, in milliseconds since January 1, 1970.,因为 GPS 时间不是 UTC 时间(由于 GPS 时间中缺少 UTC 时间的闰秒)跨度>
每当您从 GPS 接收到修复时,都会调用方法 onLocationChanged。该方法的参数是Location 的一个实例。此参数的方法之一是getTime,它将为您提供所需的内容。那就是如果我得到了你正在寻找的东西。阅读更多here
【讨论】:
getTime()得到的时间是GPS导出的UTC时间,不是设备时间。 (显然,这仅适用于您使用 GPS 位置提供程序 - 对于 wifi,我想它是设备时间。)请注意,真正的“GPS 时间”是 ahead of UTC by 16 seconds。 getTime() 返回从 GPS 派生的 UTC 时间,而不是 GPS 时间本身。
我遇到了一个关于 getTime 函数的描述:
public long getTime() 返回此修复的 UTC 时间,以 1970 年 1 月 1 日以来的毫秒数为单位。 请注意,设备上的 UTC 时间不是单调的:它可以不可预测地向前或向后跳跃。
哎呀!真的吗?即使它确实返回了一个包含毫秒值的时间对象,如果它“可以不可预测地向前或向后跳跃”,则该值是完全无用的。
我正在从事的项目绝对需要一个“精确到毫秒”的时间值(即使它是“自 1970 年 1 月 1 日以来的毫秒数”)。没有那个准确度,我就完蛋了!
【讨论】: