Android - Network Date/Time(Android - 网络日期/时间)
问题描述
我正在为 Android 编写应用程序.我需要允许我的用户捕获他们当前的位置和记录发生这种情况的日期/时间.问题是日期/时间不能是用户可以通过调整其设备上的日期/时间来更改的东西.
I'm in the process of writing an app for Android. I need to allow my users to capture their current location & log the date/time that this happened. The catch is that the date/time cannot be something that the user can change by adjusting the date/time on their device.
您能否指出正确的方向,以便在为塔使用定位服务时获取蜂窝网络日期/时间,以及如何获取 GPS 日期/时间.
Can you point me in the right direction for obtaining the Cellular Network date/time when using location services for towers, and also how to obtain the GPS date/time.
感谢您的帮助,谢谢!
推荐答案
可以使用 LocationListener.
请参阅开源 GPSTest 项目以获取完整的工作示例:https://github.com/barbeau/gpstest
See the open-source GPSTest project for a fully working example: https://github.com/barbeau/gpstest
您可以请求收听任一网络位置更新:
You can request to listen to either Network location updates:
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
或 GPS 位置更新:
or GPS location updates:
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
然后,当一个位置被传递到 LocationListener.onLocationChanged 方法时,您可以从该位置读取时间戳:
Then, when a location is passed into the LocationListener.onLocationChanged method, you can read the timestamp from that location:
@Override
public void onLocationChanged(Location location) {
Log.i("Timestamp", "New timestamp: " + location.getTime());
}
另一种选择是向网络时间协议 (NTP) 服务器发出请求,该服务器完全独立于设备和蜂窝网络.
Another option is to make a request to a Network Time Protocol (NTP) server, which is completely independent of the device and cell network.
有关实现 NTP 客户端的详细信息,请参阅这篇文章:
See this post for details on implementing an NTP client:
使用ntp服务
这篇关于Android - 网络日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!