Is it good practice to fetch current time from internet? - android

I am making a mobile application and I need to have the current time. However, I don't want to depend on the user's phone time since it can be changed to whatever they want. Is it a good practice to fetch from a server its local time every minute or similar interval?
P.S: The app will use the time to show if a shop is open or closed so I don't mind about different timezones, I only need the time in the server's timezone.

Depending on the need of the application. If your application would require a connection from the internet, then yes. You can also put that in case there is an issue catching the online time, you can just take the user's time.
From personal experience I never had issues that the user would want to change their phone time, but there have been a few exception.

To use server times is fine. But based on that making decisions on data (shop open/not) is not a good practice. Whenever you make an api call, get the status if its closed or not. This will avoid user side data issues
If you want to show this information on a single shop page, fetch the time difference between current time and closing time from the server. This will help you to maintain the status on app side. this should not be for a very long time. You can use this solution if the difference is less than 10 or 15 mins.

Related

Accurate time without sim and without internet

I have an app which is used for collection all day users collecting amount normally offline and then in the evening they uploaded amount online. Sometimes there device date and time is inaccurate so they uploaded wrong data.
At the time of collection my app is getting date and time from the device. Is there any way that app always store accurate date and time without Sim and without the Internet.
You can create a Sqlite Table in application cache when user switch on data,you
can save current server time in that Table and in offline mode you can compare server time with current time. this is the nearest approach to get accurate time
Well, before searching for an app to do this. As a human how can you tell if the date/time is correct without internet?
A possible solution would be a service which counts seconds from the last time the device was connected to the internet (which means it had a correct date/time at that moment) then when you want to add the new amount you can add the seconds to the last time and store this value.
Edit
You will 2 things to achieve this:
BackgroundService
Counter which you can achieve in many ways, I would go the easy way. check this: how-to-measure-elapsed-time
Or you can have an AlarmManager to do this.
One more thing You need to be careful if the device goes off or for some reason, something stopped the service. All of these are user issues and you need to decide how much you want to handle stuff for the user.
You can fix user mistakes by pushing to a SqlLite database every 10~15, but this won't fix if the device went off for hours and then started again at a wrong time - I don't think there is a possible solution for this-.

Android Time Synchronization in UTC

I have an application that relies heavily on current timestamps. Currently, when user submits a request, I get the current timestamp in UTC using System.currentTimeMillis(). While this works fine, it becomes a problem when the user starts manipulating their Date/Time on their device and would result as inaccurate timestamps.
Why do it on the client then? Why not just handle it on the server? Well, my application needs to work offline. All my requests get pushed into a jobQueue when connectivity to the internet is unavailable. In these cases, I must have the original time wherein the user did the action so if I submit a request at 4:02pm, but due to network problems, server will only receive it around 7:30pm, server MUST know that I sent the request at 4:02pm.
Now what options have I considered?
Upon user login, I sync the device time with the server time and store that time locally. If any user manipulation occurs while the user is logged in, I'll have a BroadcastReceiver listening in onto any intents of Date/Time manipulation, then store the offset so that whenever a user submits a request, I will calculate the synced time with the offset to ensure the timestamp is accurate.
Have a server sync api done in the backend and set up a service within my application to continuously sync up with the server time and look for any drift while also listening in onto any user manipulation.
Use push notifications and listen downstream for time synchronization adjustments while also listening onto any user manipulation.
I could also make use of the NTP servers to synchronize time with my device.
I'm not entirely sure which would be the most optimal (assuming I have listed down all the possible solutions). If there are other solutions I haven't thought of, please let me know.
P.S. If I happen to use the BroadcastReceiver to listen onto any datetime manipulation on the device, how would I even calculate the offset in that?
It has been some time since I asked this question and there hasn't been any elegant answers to the problem so after some research and some trial and error, I decided to take the NTP route and after some digging around, I found a nice library that does the entire thing for you.
It can be found here:
NTP TRUE TIME
Credits to these guys who had made life a lot easier.
You must sync with the ntp servers just once and from there on, they will calculate the Delta for us giving us accurate UTC regardless of SystemClock time.
For time synchronization you can implement like getting time zone from server. Once we have timzone we can get the current time of server.

Android: calculating time away from app

In my app, I'd like to keep track of time away from the app, so that if the user is away for say, 2 hours, and come back, they receive 1 in game currency.
In practice I could easily do this by using a date object to get the current time in onStop, saving that date object, and comparing it to a new date object when they return.
However, I have a feeling this can easily be spoofed by change the internal date settings.
Is there a more secure way? Should I just put a cap on how many hours away you can bank up? I'd love some insight. Thanks as always.
you can try to make more secure code in the app, but the safest way is to request something from a server
if you are app is already using internet permissions consider making an HTTP request to get time.
look at this site for example
http://www.timeapi.org/
if you have backend to support your app that would be extra better
if you are not using internet permissions it might not be worth it to add it just for this, you might consider a different "offline" solution

Best way to Check remote database after fix time interval

I want to develop this simple instant messenger application. Now i can send text to database. Now i want to show a notification to the user if he receives a new message.For that i think i want to check the remote database after fix time i.e every 20 sec. What is the best way of doing that??
I know my question could end up being lame and this might not be the best technique to show user notification of new message but for now i just know that so i am implementing it . .
Suggestions are always welcome . .
Use a timer in a service, so it can occur even if the app is not in the foreground. As an aside, I wouldn't do it every 20s- far too often, if you got a significant user base it would cost you a lot of bandwidth. Back it off to less frequent- every 60 seconds is probably sufficient.

Time since the app is open in android

Hello
In my android application i would like to get Time since when the app is opened.
Initially what i tried was getting the time when the app is loaded from the server and then taking the difference with the current time from the device.But by doing that if the user changes the time then i willnot be getting the actual time.
Its not posiible to hit the server again for the same.
Is there any way to achieve this in android?
Please share your valuable suggestions.
Thanks in advance:)
Try the "SystemClock" class, "uptimeMillis()" method.
Store the result in a variable when the app starts.
Echoing what I said for your other question, you first need to become familiar with the activity lifecycle and understand the novel meanings (almost meaninglessness) of common words like "open" and "start" in the life of an android app.
There isn't any way you can prevent the user from changing the system time - you just don't have the right to do that to users. Normally this should be a rare event, unless you do something that makes them want to, such as lock them out of a free version of your app after so many minutes. (However if the phone is on a mobile network, presumably the mobile network occasionally adjusts its time to correct for errors in the device's oscillator, or administrative time changes)
What you can do is check the system time on every entry point to your application. If it ever goes backwards, well... something is going on. If the clock has been set back, you could assume no time between the calls with the negative time difference and resume your time meter from there, at least keeping all the previous used time in your record.
It may be that there are cpu cycle counters which you could query and correlate to system time, but this may be highly device specific and may in fact be resettable. And it may get weird if the cpu frequency is demand throttled.
You might be able to set a countdown timer as a bound on the maximum possible time between entry points at which you could meter. I don't know if these work reliably across system time changes or not - ideally they would. Testing or reading the source will reveal.
Use elapsedRealtime in your onCreate() store it. More reliable.

Categories

Resources