How to get device independent local date and time Android - android

My application is time dependent and I don't want change in device date and time affect my application, like if user deliberately set device date to any previous date . Is there any way to get current date and time when user is connected to mobile network or WiFi I don't want to use GPS.

Javadoc of the SystemClock class describe different ways of counting elapsed time for various scenarios and conditions.
In your case you have to use
long time = SystemClock.elapsedRealtime();
Return the time since the system was booted, and include deep sleep.
This clock is guaranteed to be monotonic, and continues to tick even
when the CPU is in power saving modes, so is the recommend basis for
general purpose interval timing.
Call it for the first time when you want to start tracking the use of Network (I assume you already know how to do it) and store that value.
When you receive event about user not using network anymore, call the same method again and calculate spent time.
long elapsed = time - SystemClock.elapsedRealtime();
Next you can transform milliseconds to String for example like this:
String formattedElapsedTime = DateUtils.formatElapsedTime(elapsed/1000);//note that this method takes second as arguments so we have to divide it by 1000

You should call webservice and get current date and time of server. Then server would be responsible for the actual date/time. Best way is to build your own webservice, because it gives you more control than using third party.

Related

Android: How to use PosAtTime with a date

I need execute an action on a specific date
Handler(Looper.getMainLooper()).postAtTime({
texto.text = "Testo ha cambiado"
},2020-02-12)
postAtTime takes a milliseconds value. You'll need to use some date library (like Date) to create an object representing the date you want. Then use its "convert to milliseconds / Unix time" method (for Date it's getTime()) to get a value you can pass to postAtTime
But if you post a message to the Handler, your app will need to stay running the whole time until the time your message is handled. If the app is closed (including by the system) that message is gone. And your app needs to be running in the foreground when it happens too. It's not meant for scheduling important events that have to happen even if the app is closed, and it's really not meant for things way off in the future
You probably want to use AlarmManager
Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.
(there's also WorkManager which isn't about a specific time but might be better for whatever you're doing)

Android: Actual Date and Device Date

This is my first time to ask here on Stackoverflow and I am apparently having a hard time on this one. How could I identify if the user changed the device's date in order to activate a date-based code actions?
I really need to compare the actual date and device date without internet connection.
For example:
When I click this button, it would only open in August 29. Changing the date would allow me to access the function of the button.
You can use a Network Time Protocol. Which provides the network time and date so it can't be tricked by the user changing the phone date.
Google has one open source at: This link
EDIT
Which provides this code as sample:
SntpClient client = new SntpClient();
if (client.requestTime("time.foo.com")) {
long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
}
where it puts time.foo.com you should put time.google.com. You might need a timeout in milliseconds to add to the requestTime(host,timeout_millis) method.
And for NTP server you can use
time.google.com
Provided Here
If you can make sure that when your app is installed it has correct time then, you may implement a listener to know manual clock change and then do what you want to do. Reference: Is there a way to detect when the user has changed the clock time on their device?.
There are 2 more options, one to get time using GPS and other is to get time from Network. Not too sure about the network, it is something NTP related stuff will explore when I get a chance. Let me know about your implementation.

Android - Get current time without dependency on device's clock

I've noticed that System.currentTimeMillis() time is device dependent. If I change the time on the device's clock, this method will return a different answer.
For example: If the real time now is 10:00, and I change the clock on my device to 9:30, then System.currentTimeMillis() will return the 9:30 time (in milliseconds..).
I've also tried this answer and some other answers, but didn't find anything useful.
I should state that my app works mostly offline.
Is there a way to get the real current time (device independent) without external API?
If it were not for the 'offline' part, I'd have suggested to use a time server, but given that your app is offline most of the time that might not be a good solution.
If you don't need the actual time but just a time that cannot be messed with, you can use SystemClock.elapsedRealtime() which gives you the time since the device last booted.
You could also combine time server and SystemClock.elapsedRealtime(): Fetch the time from timer server once (e.g. after bootup) and from then on add elapsedRealtime() to that initial value (minus the elapsedRealtime value of when you get the timerserver value).
If you use the GPS location provider, getTime() will return the UTC time derived from the GPS signal, rather than the device time. The GPS location provider can work offline - but it will be much slower to obtain a fix compared to being online when it can access the A-GPS info.

Measure long elapsed time (with reboots) on Android

I need to measure long elapsed time on Android and there may be device reboots in between.
From what I've understand, System.nanoTime() is resetted every time the device reboot, and System.currentTimeMillis() is unreliable because user can change it.
The only solution that I came up with is to listen to ACTION_SHUTDOWN and BOOT_COMPLETED, use System.currentTimeMillisec() to calculate the elapsed time (user can't change clock time while the device is off, hopefully :) ) and add it to the last System.nanoTime() I had before shutting down.
I honestly don't like this solution because it's very expensive (I need to listen to 2 broadcast events) and inaccurate, but I couldn't figure out any other way to do this.
Any ideas? Also a native solution would be good for me.
You can get around the user changing time by using an internet time server to get the times when you check. There are a couple of ways to do this.
Get it via NTP server
How to get current time from internet in android
How can I get the "network" time, (from the "Automatic" setting called "Use network-provided values"), NOT the time on the phone?
Get it via HTTP header
http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses ( see Date header )
If you simply persist this value then the user can do nothing to mess up your calculation.

Get the real time passed on Android

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

Categories

Resources