How does Automatic date and time work in Android? - android

For some date-based calculation I needed today's correct date, for which I enabled Automatic date and time option in Date and Time Settings and this gives me the correct time and date.
I want to know how Android gets the correct date and time, even though I have not enabled any internet accessibility (WiFi, SIM Data).
Also look at this: There is no response from requestLocationUpdates
Please give me some reference on this.

It capture date and time from the network provider. not only android lots of other phone can do the same.
Autometic date time is provided be the network provide (exmp: vodafone,airtel etc etc ).

Related

How to get current time from network time protocol?

Actually, in my application, some login expiry time is there but if the user changes device time, date settings it is not working properly, I wanted to get network time using NTP. For this, I am hitting google.com, but this is also not the proper way as per location coordinates. So is there any way to get accurate time based on location using NTP
https://developers.google.com/time/
No, NTP does not use location specific time (timezones). It uses UTC.

Get TimeZone from internet in Android

I am trying to get time from the device but it returns incorrect time if I change my timezone (Because of the timezone). Is there any way to fetch timezone from the internet so that If a user changes its device timezone it should show the actual real time.
BTW I am using the true time library to get real UTC time.
You can use a publicly available API for this. I use http://ip-api.com/json go thorugh documention before implementation. However if you want you can use other APIs. As I am sure there are many.

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.

One month restiction for application free use

Requirement:
In my application I want to allow user to free use all features for one month from the day of application's first run. For this I have store first run date in application database and compare first run date with current date every time when application launch.
Implementation:
For getting current date we have several function like Calendar.getInstance(); Date date=new Date(); these function will return the date\datetime of the standard "wall" clock (time and date). According to document Which can be set by the user or the phone network.
The Problem:
Every thing seems to be work fine but what if user change date from Setting and set it to past date. For example user have first run application on 7 June 2013 so after 6 July 2013 application must show that he have to purchase subscription, but if user change date of device back to 30 June etc. then the restriction will not work.
So is there any why to implement it correctly?
Is there any why to get actual time of device that is not be editable by user?
Edit:
Application can work offline, so user can turn off internet connectivity.
How about storing two dates - the date the app was first used and the date it was last used. 30 minus the difference between the two dates will give you the number of days remaining and if the current date is earlier than the last used date you know that they have changed the date back.
Just an idea - not tried it but in theory it should work.
Store the first day it was used and the last day it was used, and listen to the ACTION_TIME_CHANGED intent. Also, try using an AlarmManager.
Or if you have network, store it in a database off the phone.
But you probably need to ask yourself if someone really would change the date of their phone just to use your app. It would seriously mess up a lot of things for them: alarms, calenders, syncing to other services and so on. I don't think it is a problem in reality.
If it's a possibility, you could use a one-shot GPS request to get a time value. The user can't change the GPS time that it receives. Adding the permission is a pain if you don't need it, but this would prevent bad behavior caused by the user mucking with the date/time.
Event(APP_INSTALL) -> storeOnServer(INSTALL_TIME_KEY,getTimeFromServer());
Event(APP_LAUNCH) -> IF(getTimeFromServer -getStoredTime(INSTALL_TIME_KEY) >30)
-> showTrialPeriodEndedAlert()
-> quitApp();

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