Android: calculating time away from app - android

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

Related

Is it good practice to fetch current time from internet?

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.

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 - Implementing a means to get 'real' time in offline apps

Background
The company I work for is creating an app that collects information from various device events and sensor data.
One of the things we would like to be able to do is use time to process the data when it gets to the server.
We would also like there to be an acceptable amount of "offline time" allowed, i.e., the app could be used for its purpose even while offline until the next server data update is required etc.
Problem
One issue is that the user can just change the device time.
We overlooked this, and have been able to successfully generate events for the previous day, which would mean that our app could be fooled in situations where device time is a factor, which is very uncomfortable.
Imagined solutions
Obviously I can ensure that the app is always sync'd to the server before it starts collecting important data, and then use server time and make all further time relative to that.
Another way might be to keep a reference to something like the last update time or even the app install time and work out time relative to that.
Questions
How do people get around this typically?
Are there any libraries out there that can be used to enforce real device time?
Thanks guys

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

Categories

Resources