How to get the global time (not system specific time) in android - android

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.

Related

Mobile App: how to get correct time when offline

We're building an App using Ionic/cordova that will need to run on Android, iOS and Windows 10 Mobile.
The goal is to register start and stop events, we'll need the exact time and location when this events are registered.
There is a serious concern that the users will modify the timesettings on their phone to send false data.
As long as these events are sent immediately to the server, we can use the server time to validate what the user is sending us. But there is also a requirement to be able to work when offline.
Our first thought was to use the GPS time. In cordova the position object has a timestamp.
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-geolocation/
But after further reading, here and here, that this isn't necessarily the time of the GPS server but also just the local time. I've read about work arounds using the uptime of the device, but this won't work as soon as there is a reboot. But most of these conversations are a few years old.
For now we think the simples approach will be to flag events that are sent offline as 'suspicious'.
Is there any other way to accurately determine the time when you're offline without risk that the user tampered with it?
Haven't tried it myself,but what about getting the time from the Network Provider, as said in this SO: I want to get time from GSM network
long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();
Time returned by getLastKnowLocation could be old if you want current
time use
locMan.requestSingleUpdate() and pass the network provider
Though, this may not work in iOS or Windows.

How to get real time date from server in android?

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.

Retrieving the current date and time from a time server

In my Android app, I would like to retrieve the current date/time (UTC) from some server. Android does have the built-in ability to synchronize the device's date and time with some server but I have found this to be unreliable. Does Google or some other well known service provider provide a simple API that would allow me to retrieve the current date/time? I am not interested in trying to set the date/time on the device (that's not even possible). But I do need to know the correct date in order to perform scheduled tasks that depend on exact dates.
I have seen a device reset its date to something like 1980 when the battery has been removed for an extended period of time and therefore I cannot rely upon the date of the device.
EDIT:
I came across this post:
https://stackoverflow.com/a/13066268/753632
But it requires installing Apache Commons Net. Kind of overkill just to get the time. I don't need to bloat my code more than it already is.
you can refer this link How can I get the "network" time, (from the "Automatic" setting called "Use network-provided values"), NOT the time on the phone?
You could use the calender class, like is explained here, although this uses the time of the device itself. You could also read this it explains how to get the time for the service provider.
I think using the calender and then calculating the UTC time with based on the timezone the phone is in (like this). Because the phone already gets it's time from a server when it is connected to the internet.
I hope this helps.
EDIT: I see you edited your post. So you don't want to use the device time... I think you could look into getting it from the service provider then.

How to update the android time

I am using android TAB for some app development .
in my application I get UTC time from the other application(other device provides to my application via TCP socket ) . Once I receive this I need to update the time with receive time . Is there any apis to change the system time ..?
public static boolean setCurrentTimeMillis (long millis)
Sets the current wall time, in milliseconds. Requires the calling process to have appropriate permissions. [although Only system process can change the time]
User apps cannot set the time. Only the system processes can. This is
for
security, but also because I think it won't have much effect-- the time does reset from
the radio via network time.
reference link

Get accurate time from android/iphone to server

We have an android(or iphone) client we are developing. The client allows the android user to send entries to a server which we also develop. If the client does not have data services (GPRS) at the moment the user sends the entry to the server, the client also supports saving the entry to an offline database and sending it later to the server.
One important aspect of the whole process is accuracy of the timestamps on which the user sent the entry to the server (whether the entry is made in real time or sent by the client from the offline database)
When available on the client, we get a GPS location and are able to use the GPS timestamp to send that to the server (or save the GPS timestamp on the offline DB and send it later to the server). However if the user has turned off the GPS (and all other location services), the device will not have a GPS fix and therefore the server can not determine accurately when an entry was made.
We can not use the local device clock as the user may change the clock to make entries on different times than they actually occurred (these entries are part of the users salary so he might have an interest to "fix" them).
So basically I am searching for a way to determine as best I can the time some entry was made when I can not trust the internal clock of the mobile. The algorithm should support both entries sent in real time or entries sent from an offline DB. the algorithm should also support cases where the user changes the time of the mobile, turns the mobile on/off, turns the GPS on/off while the application is running on the mobile etc...
Few ideas that I thought of:
Although I can not trust the mobile's time, it can still perform as a stop watch:
Have a class that will loop until the application exists, the loop will sleep 1 second and increase an internal clock variable by 1 second. On every GPS location my code gets we update the internal clock variable. This way I have an absolute clock that came from outside the device (from the GPS) and when the client sends an entry to the server, we can use the internal clock as an absolute time.
PROS: the user can not modify this clock as it is only updated when we get a location from the GPS
CONS: the application needs at least one GPS fix before the user can make any reliable entries
I can take advantage of the fact that the server has an accurate clock which is correct. If the client would send to the server info that the age of the entry is 10 minutes, the server could use its internal time and know the exact time the entry was made on.
The biggest problem is how to know the entry age? I thought about saving the entries to the offline DB with an age of 0, then every 1 second increase the age of the entry in the DB. The problem is that if the app is closed and/or the device is off this will now happen
This is where I am currently stuck. Any ideas on how to solve this are more than welcome
Thanks
Here's how I handle this issue for iPhone. When the app starts, I call my server and ask for the current GMT time (you could also call a public NTP server if you preferred). I then compare it to the system time. If it is different by more than X then I popup a message saying, sorry your system time is wrong so you can't use the app until you fix this. I then monitor for the user changing the system time while the app is running and if they do that, then I do the compare again (and popup the error message if the time is off by more than X). This ensures that their system time is always correct (within some reasonable allowance) and you can trust [NSDate date]. However, this solution does require a valid network connection. If this solution works for you, I can post the sample code.
i think i am going to combine Jules and Joel's answers into one solution which will provide for my needs the best solution:
since the user might change the clock when the mobile doed not have GPRS, just detecting the time change event will not help us as we can not validate at that moment the new time is correct.
As Joel recommended i will pull the time from my server when my application is started (at that point i still must have communications with the server or else my application will not start). The time pulled from the server along with the current device upTime will be saved.
when the user wants to make an entry i will calculate the current time using (Server Base Time + Current UpTime - Base UpTime). this way i will have an independent source of time regardless of the current clock of the device
this will defenitly work on android
on iPhone we will try to use something out of http://www.cocoadev.com/index.pl?FindingUptime to get the upTime
Jules & Joel, thanks for your answers!
Look into android.os.SystemClock. Specifically, elapsedRealtime() returns a time since the phone was switched on, which is not affected if the clock is changed by the user.
You can correlate times in event the phone is switched off by having code that runs when it is switched on and checks the realtime clock. As the clock can't be changed when the phone is off, I suspect you could use this to put together a system that will catch any simple attempts at cheating. (If the user roots the phone all bets are off -- they could modify the behaviour of the APIs from under you).
Running code every second will kill the phone's battery life. Most phones would be unlikely to last a day if you did this.

Categories

Resources