How to retrieve provider time in Android programmatically?

I am doing an android app which has to display the current server/network operator time since the device time can be changed by the user. I don't want to change/set the device time. I tried few source codes but they didn't work well. I'm able to get device date and time but I need date and time from server or network provider itself. please help me

Answers


It seems that you need to interface with the Network Time Protocol (NTP).

There are similar questions here on SO use of ntp service, get datetime from network provider (as noted by Gheo) suggesting to:

As noted in the comments of these posts, as of Honeycomb you cannot do any network operations in the main activity thread (suggestion: use a service instead).

Update:

A further search led me to find Dominik Schürmann's ntp-sync repository on GitHub. He provides different examples of how you can use his library which you can compile and run on your emulator/device.


I had found a solution but forgot to post it. In case somebody finds it helpful, here is what worked for me.

Get the server time using an API or network call wherein the response will have the timestamp from the server. Use the data as per your convenience.

You can modify the existing API itself, if any.


Need Your Help

What assumptions should code make about CPU memory model, and how should such assumptions be documented?

.net multithreading memory-barriers memory-model

From what I've read, Intel processor architectures enforce a stronger memory model than .net implementations are required to provide. To what extent is it proper for code to make use of the guaran...

What is the difference between function(vector<int> v) and function(vector<int>& v)?

c++ function vector

While trying to perform insertion sort using vectors in C++, I discovered that when I used, it didn't perform the function: