Get correct date if user offline android java [duplicate] - android

This question already has answers here:
How can I ensure in Java that the current local time is correct? [duplicate]
(3 answers)
Closed 4 years ago.
How can I get correct day from Android device. Now I have a problem to get the correct day if user is offline. If the user has no internet connection and the user has changed his date in device. How can I get the correct date like whatsapp?
Some ideas? I have already tested get date from location.
update
I want to get correct date regardless of user has changed his date on device and the device is offline.

I assume your goal is to prevent a user from bluffing their date backwards.
Thus extending an otherwise free period of usage?
If so, the answer is simple. Add a sharedpreference that stores the active date whenever the app starts up if the device has internet.
If the device does not have internet, then check your last known sharedpreference usage date and see if the device date is before that. This will at least let you know that they rolled back. Its not perfect, because if they decided not to use your app for a month, then rolled it back before opening, it could still give false positives of looking like they are ok to use it. However, it is very unlikely situation.
The most likely situation is they will open the app, get a warning that their date exceeded usage, and then they will try to cheat by going offline and changing the clock, at this point you already set a flag that they expired, or you know the last usage date. Either way you have the information to prevent usage.
You could also just require internet access for the app to be used, that's what I typically do to avoid being cheated on trials.
If that is not your goal, please elaborate more so we can help you with a better solution for your situation.

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-.

Get datetime/timestamp from a website/url in android

here is what i'am trying to achieve, either one of the two would be ok
Get the date and time from a specific url/website like "www.mywebsite.com"
Get the date and time from my MSSQL Server
either one of the two would be ok, because all i need is to get the same time from the website/server's end, because i'am making an APP Version for the Mobile Website i'am making and i want to show a Clock or Date&Time Reminder in the App
i have no codes working as of now, i tried searching online in search engines and also in stackoverflow but i can't seem to find what i'am looking for, either i'am using the wrong words to search for it or there really isn't any way
the reason why i need the date is because i'am checking when the records inside the database was created and verifying how much time has passed since it was created, it like comparing (CreationDateTime & CurrentDateTime)
i can successfully Display how many hours have passed, i just need the CURRENT PROPER & ACCURATE DATE & TIME of the server :)
I can't help but think this is an XY problem, or at least close to it.
Your client device knows what time it is, because it stays in sync via NTP and whatever network it's attached to (and we already know you're attached to a network because otherwise, this whole thing falls apart). And it knows (or should know) what timezone it's in, so you can translate that to UTC or whatever alternative timezone you might need.
Your server(s) should also be keeping time synced via NTP, so drift between the client and server should be sub-second at worst.
IOW, the time (when corrected to UTC, which all date/time libraries I've used have a method built in to do) will almost always match between your client and server, so why bother with the complexity of "pushing" the current time from server to client?
I'm not seeing why you'd need to do this at all. Have the server tell the client when the reminder is set for, then let the client work out the rest. Anything else would be excessively chatty over the network.
To get the date in the format below, just convert todays date to a string
--07/06/2017
SELECT convert(varchar(10), getdate(), 103)
i have found an alternative answer that works 100% in getting date and time from SERVER instead of URL
SELECT GETDATE() AS CurrentDateAndTime
GETDATE()

Android: How to detect if user changes system time without running a constant service to check time

In my app I want to allow user to be able to use it for 7 days offline usage for free, but want him to connect online once 7 days are done and buy a subscription after that. So basically, I want to lock him out after 7 days from install. The problem is user can very well change system time and trick the app. Also, I cannot use server as the authority as the app is supposed to also work completely offline, except for the time of purchasing subscription.
I can run a background service to keep track of any system time change but I want to avoid doing that.
Is there a straight forward solution which can work even if the device is totally offline for 7 days.
You can save the current time in shared preference and compare it every time you sample it. If you get an older sample the user changed the time backward.
Use remote clock , for even simple read the time of server from mobile in desired time gap and use those number for your app's clock
So this is how I am planning to solve this, though this is not unbreakable but will work for majority of users.
So while signing for trial I get network time and store it as start_time in preferences. Next on each app start and app resume I read the current_time and write to prefs. Once current_time of pref is greater than time of device, we lock the app. Once preferences are not there we ask user to come online as suggested by #Zach
To make preferences editing a little inconvenient to the rooted device guys I am storing data in encrypted form in prefs.
This is still no way unbreakable as one can decompile the apk and read the obfuscated code to guess the algorithm and the key but I guess the value of content in the app is not that high for someone to go through that much pain.
can add syscall and track the time offline, get it from here

Android: How to verify if the date time provided by the system is correct? [duplicate]

This question already has answers here:
How to check "Automatic date and time" is enabled or not?
(6 answers)
Closed 2 years ago.
How do we verify if the date/time provided by the Android system to our app is correct?
We cannot use network activity, we have tried doing it using a server, and it's working nicely. But need to check if there is a speedy way we can do some offline checking on the phone's provided date/time?
Buddies, figured out the answer
First we need to check if Automatic Date/Time is Enabled or not from here,
How to check "Automatic date and time" is enabled or not?
If enabled, that can mean the date is be correct (mostly)
I believe there isn't a built in API to check whether the system time is correct or not by comparing it to a server for example. However, it should be simple to implement a query that checks a server to check the current time and compare it to the system time.
If your phone is sometimes online you could cache the last date and compare it.
If the Auto update is enable, I think you could trust it.
Else, you could do some ultra ugly stuff to vedify date is not total corrupted.
I don't think there is a stright way to do this, sorry.

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.

Categories

Resources