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
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 am developing and app that need to access to actual device hour frequently. I can detect if device hour is correct or wrong comparing with server time, if device hour is wrong I need to change automatically to real hour.
I've seen that to change the time of device programmatically is necessary to have rooted device.
Is there any way to force synchronization of the time when you know it's not right that does not require root?
If not possible. Is there any way that my app has a internal clock different to clock of the device (in sync with my server), to not call the server every time I want the time and to get the correct time when even I have no Internet?
For the third-party app, there is no way to set system time without root permission.
Yes, you could use Calender and the following intents broadcast from system to build a internal clock. Another way to simplify the work is using TextClock widget directly as an internal clock and use reflect mechanism to set and track the time of TextClock if you don't want to build your own clock. As long as your app keep running, the internal clock will work.
Intent.ACTION_TIME_CHANGED
Intent.ACTION_TIME_TICK
Intent.ACTION_TIMEZONE_CHANGED
Can I synchronize one time on several Android devices?
For example current time is 00:00 and through the timeapi (web service) we sent this time to Android1. Due to some delay we get 00:01 instead of 00:00.
We make the same on Android2 but in this time, delay = 2sec and we get 00:02.
So can we sync these time between 2 Android devices?
Maybe you suggest another way without web serivce, via bluetooth or sent several requests to web service every 5 sec to calculate average delay?
At best I have to get 00:00 time at Android1 and Android2.
If what you want is the correct time on all devices, then you usually don't need to do anything except make sure that the built-in time synchronization is not turned off, and the time synchronization will be done via the NITZ protocol.
If your carrier doesn't provide NITZ time sync, you could use NTP.
If you want to have your own, custom time, turn off the NITZ sync, and use NTP with your own custom NTP server.
I'm developing an application, which will have a custom yearly subscription license. I need to know exactly how much time has passed. The user could keep the device offline, therefore I can't check the time through internet. The user could turn back the clock, therefore I can't be sure of really passed time. Is there a way to get the real time elapsed?
I think you can use System. nanoTime(), which can help you measure an absolute elapsed time (as opposed to System.currentMillis() which will be adjusted if the system clock is changed).
See the nanoTime and currentMillis javadocs for more information.
ps: I have not tested it.
you can have a preference or database that need to be stored the time when user install the application ... and you will always compare the time passed with the difference between stored time - current system time ........
or may be make a service to get network time
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.