android get actual time - android

I am designing an app. I am facing this one problem. I want to get time from the user's device even if the user has changed the time(which is not the exact correct time).
+
If the user changes the time or time-zone, somehow; the application should notify the app.
However, I need to check this offline. Please suggest me a suitable way to tackle this Candy crush-like scenario in my app.
Thanks in advance.

You can use the UTC time. System.currentTimeMillis() REF

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

How to identify when the Android device was used last time

I am writing a piece of code in Android that will try to identify when was the last time the Android device was used by the user. By used I mean any operation performed. If the device is not used at all for lets say 1 full day I want to perform some action for it in my service which is running. So basically when the device is not getting used (idle state) that time I want to identify all this.
Is it possible in Android to achieve this? How can I proceed further for this requirement.?
Any pointers will be helpful to identify the device was not used for X days(or hours) and perform some action on it from idle state.
Thanks!
Please follow this tutorial for screen turn On/Off. Hope it will helpful to you.
How to identify when the Android device was used last time?
When first time screen will turn on then you can read system current time and save in shared preferences and from second time (system current time - read last holed value in shared pref) and convert these milliSec to time.

Android - Last screen lock time

Is there a way to find the last time or the time spent since the user locked/unlocked the screen? Is there any log which stores this information?
I am trying to track user (in)activity and looking for the simplest possible way to do it. I know it can be done by continuous tracking through a service perhaps, but was trying to avoid doing that.
It seems that this cannot be done.
I ended up creating a new service which track the last screen lock/unlock and save the time myself.

Time in android

In my android application I would like to block the user from changing the device time when my application is opened or minimised. The user should be able to change only once the app gets closed.
Please share your valuable suggestions.
Thanks in advance :)
Android applications do not get closed or minimized, they have different semantics. Please read up on the activity life cycle.
Android applications do not get to block users from conducting other operations on the device.
I would like to know how long is my app open so i thought i would take device time and by taking the difference i would get the time.
There is an API that will tell you how much time has passed since the device was booted, regardless of what the device clock is set to:
SystemClock.elapsedRealtime() "returns milliseconds since boot, including time spent in sleep."

Categories

Resources