3

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.

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.

Any help is appreciated, thank you!

Chris
  • 91
  • 2
  • 7
  • 1
    Are you logging information server-side? If so, just use the server date/time. – Brad Nov 07 '11 at 17:45
  • We are doing this, but the requirement is actually from the government. Their requirement is to use the "GPS Signal Date/Time" or "Cellular Network Date/Time" (depending on how location was retrieved). We are having a similar issue w/ our iPhone app... but I'm not the dev for that :) – Chris Nov 07 '11 at 18:16
  • I'd use a GPS hooked up to the server for this. It would be harder to tamper with, provided you aren't doing delayed uploads. – Brad Nov 07 '11 at 18:31
  • But do you know if there is a way to retrieve the cellular network date/time and/or the GPS satellite date/time? – Chris Nov 07 '11 at 22:28
  • No, I don't (which is why I didn't fill in an answer below). I was just suggesting an alternative, in the event nobody suggested a proper answer. – Brad Nov 07 '11 at 22:32

2 Answers2

4

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

Community
  • 1
  • 1
Sean Barbeau
  • 11,496
  • 8
  • 58
  • 111
0
 public static final String TIME_SERVER = "time-a.nist.gov";
 public static void printTimes() throws IOException {
        NTPUDPClient timeClient = new NTPUDPClient();
        InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
        TimeInfo timeInfo = timeClient.getTime(inetAddress);
        //long returnTime = timeInfo.getReturnTime();   //local device time
        long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();   //server time

        //Get Current Time
        Long tsLong = System.currentTimeMillis()/1000;
        String ts = tsLong.toString();
        Log.e("After get PrintTime..","After get PrintTime..>>"+ts);

    enter code here
        Log.e("getCurrentNetworkTime", "Time from " + TIME_SERVER + ": " + returnTime/*time*/);

    }