Android - Network Date/Time

前端 未结 2 1275
醉话见心
醉话见心 2021-01-14 08:24

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 t

2条回答
  •  滥情空心
    2021-01-14 09:09

    GPS or Network timestamp can be retrieved by using the LocationListener in the Android Location API.

    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);

    or GPS location updates:

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    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());
    }
    

    Another option is to make a request to a Network Time Protocol (NTP) server, which is completely independent of the device and cell network.

    See this post for details on implementing an NTP client:

    use of ntp service

提交回复
热议问题