3

For an industrial process application we need to have a server initiate a manufacturing activity which is stopped or completed with a command from the application on the Android device. When that occurs the app sends a message to the server with the time of completion.

For this it would be good if it was the "same time" on the Android device as on the server so the time that the device sends the server is correct from the server's standpoint. (a second or two of network latency won't matter)

And just a clarification - many of our company's customers are defense contractors or other security conscious firms so our device typically has no access to outside networks, so NITZ or NTP options may not be available. The "server" we're talking to is just an ordinary PC controlling some industrial machinery and talking with Android with WiFi via a local wireless-access-point.

Years ago (e.g., How to set time to device programmatically ) it was stated that a non-rooted could not programmatically set the system time. Is this still true in later Android versions (we're running 5.1)? Is there any way to set the time for just our app? Any other way to do what we want?

user316117
  • 7,971
  • 20
  • 83
  • 158

2 Answers2

0

I'm going to assume this is a closed system and you aren't installing this app on random or untrusted devices (if you were, I'd suggest not doing anything time based on the client at all). In that case, rather than setting the system time (which you can't do), you can keep a time delta stored in your app- the time difference between the server and your app. Then add that to all times sent to the server.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • You are correct that it's a closed system. It would be good to display the time of completion on the device, and for that time to agree with what the user on the PC sees. – user316117 Nov 02 '17 at 15:29
  • Make the app fullscreen (so the top bar goes away) and draw the clock yourself? – Gabe Sechan Nov 02 '17 at 15:30
  • It is fullscreen and the app "owns" the display, the home button and everything. It's the closest thing to kiosked we could make it for that rev of Android. But I'm unclear on your suggestion. The server says "start NOW". We only know what NOW is on the device but when the user taps "stop" we need to say what time we stopped in terms the PC understands so we can display it that way. Is there a library that does "time math"? It wouldn't be that hard to write one, but does one already exist? – user316117 Nov 02 '17 at 15:40
0

For our application we maintain a 'server time on the client' as follows:

  1. every 5 minutes we get the server time from the server (via an endpoint that provides date, time and timezone)
  2. when we get the server time, we calculate the deviation between the server time and the local time on the device.
  3. Whenever we need the current time in the application, we take the current local time and add resp. substract the last calculated deviation.

Additionally we have a BroadcastReceiver in place that is called, when the device's time is changed (e.g. by the user via Settings). In that case we immediately perform step 1 and 2.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • It may come to that but it sounds cumbersome and kludgy. It's hard to believe that with Android being used for so many commercial and industrial uses that in in 2017 there's not some intrinsic way to synchronize time between the device and server. – user316117 Nov 02 '17 at 15:34
  • If you're talking about a rooted device- you can consider running NTP, as android is based on linux. That might work, the only question is if the framework will pick up the changes in time. – Gabe Sechan Nov 02 '17 at 15:44
  • It is not just Android. Is there any such thing built in Windows or iOS? By default, Android gets its time from WLAN or the telephone network provider. But the user can turn that off and set his own time. There is no way you can change that, so you'll have to cope with it. – Ridcully Nov 02 '17 at 15:55
  • I don't know about IOS but in Windows you can set the time programmatically. My understanding is that you cannot do this in Android unless you're rooted. – user316117 Nov 02 '17 at 15:58
  • Do you really need to set the time of the device? As I understand it, it is enough that your application is in sync with the server, which can be achieved as described. – Ridcully Nov 02 '17 at 16:05