Now I write android application that related with server and I don't know how to get time from server. How can I show server time in my application?
Thank you for your help.
get server time which return from a network call. then setTime in your calendar instance.
Related
I would like to get the current global time in android, meaning, even if the user changed the system's time, I would like to get the correct time.
How can I do that?
To do that you must first have access to the location of the device.
You can connect through a web service such as:
https://timezonedb.com/
https://www.worldtimeserver.com/
Through its APIs you can know the date and time of the zone in which the device is located. If you need more information on how to read restful or xml file of the web service answer, tell me.
The best solution is:
If you have a server. Make your own web service that verifies that the date and time of the Android device on which your App is running matches that of your server.
I want to do an Android app that submits data to a web server. This application needs to run offline and the hour of the submitted info is crutial. We can't rely on the hour provided by the client, so we are always setting it in the server side. But, when android app goes offline, we need to keep a private clock separated from the system hour (because it can be modified by the user). My question is how can i achieve this? The first solution that cames to my mind is to keep a private clock in our app, but this is going to crash when someone closes it or when shutdown occurs. There is something done to achieve this in Android? Also, we are going to use Ionic framework (suggestions accepted ;))
Thanks.
You can achieve that by using SystemClock.
It allow you to know the time elapsed from the System startup.
Store the server timestamp and wake up you're app with AlarmService. Then check if the elapsed time is correct.
Disclaimer: I am a backend developer, and I have no idea of what happens inside an Android or iOS app, so please bear with me.
In a specific use case of our platform we would need all client applications (Android and iOS) to keep an internal timer accurate and in sync with an external source of time (so that they can accurately timestamp messages sent between one another). Is this possible at all (using, for example, an NTP client or a different / better technique)?
When the client connects to the server it can fetch what the server's reference time source is. Once it has obtained this it can calculate and store the difference between the server's time and the device's time.
Then when the client needs to do something based on the time* on the handset it takes the server's time into consideration when doing whatever it needs to do such as schedule a timer or whatever.
*You can't really do anything based on time in iOS, only if the app is in the foreground. An exception is posting a local notification. Its not possible to schedule a timer for example if the app is in the background.
As per Martin H, you could use an offset from the device's internal time. The client device will probably be within a second of current time if the user has not manually set the time (which does happen - I just read about a user that changed her date/time to tomorrow to get a reward in a game).
I have dealt with this in a time-based app by using the device time. The client and server validate against server time when communicating with the server.
For example, when the client and server communicate, the server can validate the client time against server time. If the user time is off by more than 'x' minutes, the server sends back an error message.
It will be difficult to keep all clients within a few milliseconds of each other. One idea is to use the server to coordinate messages (messages are not sent between devices, but to the server, which then sends the message on). Then, you can use the received time at the server as the basis for the message time.
I am developing an android application. I need to solve the below mentioned issue in my application:
1) The application can work in online as well as offline. The app has a feature to create and save the NOTES inside the application. Also, When the app gets internet connection, I need to send the NOTES to backend server. I have a field called 'DATECREATED' in each NOTE (the datetime where the actual NOTE was created)
ISSUE:
If the User has set the DateTime wrongly in the device, My application sends the incorrect DATETIME to server. I am wondering how could I solve the issue?
Any Ideas would be appreciable.
When you eventually post to your server, just add that specific time as well. new Date().getTime() will give you a value that you can use to compare with the datetime of your server. If it's for example 2 hours later, then just set the time of the post 2 hours back.
So send both the time when the post was submitted by the user and the time when it was actually sent.
You can get current date and time by using network time.
LocationManager locManager = (LocationManager) mContext
.getSystemService(mContext.LOCATION_SERVICE);
long lastSyncTime = locManager.getLastKnownLocation(
LocationManager.NETWORK_PROVIDER).getTime();
I have used it to get time, based on location in my application.
I think it will work in online and offline mode as well.
I have one android application which will show random Quotes everyday from application's
database, but when i change date in my android phone, i will get the future quotes. To stop this i need current date and time from server side ?
Is there any URL or web service which gives real time data ?
Any help will be appreciated.
You can check this service : http://timezonedb.com/api. But its not recomended to use an external service for that.
You should probably use your own server.