0

I need to create two clocks - one that runs off of network time and one that is local to the device's hardware, such that if you were to unplug/take the battery out of my device, leave it out for an hour, and then turn it back on, the network clock will sync to the network and display the correct time, but the hardware clock will need to be be reset (probably by using this network time).

MY QUESTION

I was looking into Android's TextClock widget and I think I can use it for one of these two clock examples, but I can't find any documentation saying how the widget gets its time.

Can anyone help, and possibly suggest an alternative (ie a network clock if this is hardware, or vice-versa)?

Michael Kemp
  • 300
  • 2
  • 17

2 Answers2

1

From the source code:

private void onTimeChanged() {
    mTime.setTimeInMillis(System.currentTimeMillis()); // mTime is a Calendar
    setText(DateFormat.format(mFormat, mTime));
    setContentDescription(DateFormat.format(mDescFormat, mTime));
}

This is hardware time. You might want to look at this answer for a way to get network time.

Community
  • 1
  • 1
Nicolas
  • 6,611
  • 3
  • 29
  • 73
0

TextClock, as evident from the source code, simply subscribes to system clock specific broadcasts (namely, ACTION_TIME_TICK, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED) and then uses system time (System.currentTimeMillis()) to update its value.

What you probably need is an NTP synchronization. You may use this ready-made client for that. There is a Google public NTP server available at time.google.com.

SqueezyMo
  • 1,606
  • 2
  • 21
  • 34