I can get the time change events with the help of broadcast events (ACTION_TIME_CHANGED and ACTION_DATE_CHANGED).
I need to get the previous time after time change. For example, the current time is 10:00. I am going to change time to 12:00. After time change, time will be changed to 12:00, but I need to get the previous time (10:00) at the time of time change.
Note: Time can be changed from any other app or from settings.
Maybe the solution I found can help you:
(Is there a way to detect when the user has changed the clock time on their device?)
The above solution could work setting a static variable on BOOT of device with System.currentTimeMillis(), and sum with SystemClock.elapsedRealtime(). With this sum you have the before time.
But there is a problem (I don't know if it matters to you), if the user does not reboot the device, then your diff will be == 0 and you will not be able to have this diff between old and actual System.currentTimeMillis().
At the time of change timing task just store current or old time millisecond in sharedpreferences i.e u can use any time even app will not in stack it self
Related
I want to run a function from my Android application at a fixed time (let's say 8am) on the first day of every month. The function, depending on user's preferences, will either serve a notification or start downloading a file over the Internet (both of which are already taken care of) or do nothing.
I tried using BroadcastReceiver with action android.intent.action.DATE_CHANGED but I read that it is fired only when user changes the date manually (although even then it worked the first 2-3 times I tried and then stopped working). I think an AlarmManager will be able to do what I need done but not really able to figure out how to implement it.
This should run whether or not my application is active/running. What is the optimum way to do this? Any supporting code will be appreciated.
Just start a service when start up. In the service,you could check the date in another thread.
Use an alarm
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()/*now*/, TIME_REPEATING, pendingIntent);
The TIME_REPEATING will be your calculate of the first day on the month, and the pendingIntent your service to run the task.
The unique hard work will be know how many time need to pass to the first day of the next month. But it's possible and very pausable to calculate.
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.
I want to design app in which I can get the time before the user has changed to any new time.
I am using
android.intent.action.TIME_SET
To know that user has changed time
eg. Suppose current time is 10.00 pm User changed it to 09.00 pm So can i get previous time that is 10.00 pm
Question was asked some time ago, but I have generally the same problem and cannot find reasonable (efficient) solution.
Just to recall, I want to know the time that was set before user changed time. For example, user changed time from 6:12 PM to 3:21 PM. To the best of my knowledge, there is no information about previous time in android.intent.action.TIME_SET. But when handling this intent I want to somehow know that the time was set to 3:21 PM when there actually was 6:12 PM.(let assume the same day).
According to accepted answer to this question:
android detect user modifying device clock time
If I want to handle situations when network is not available, I have to kind of append my own timer to my application, measure time on my own, and when handling
android.intent.action.TIME_SET
get old time from this built-in timer.
But it seems to me, that this is quite heavy solution and I reckon there must be simpler way to do this.
So the question is, what is the best (most efficient and the most simple) way to handle situation described above. Despite this problem seems to me very common, I couldn't find fair solution.
You could try to monitor the difference between System.currentTimeMillis() and SystemClock.elapsedRealtime(). The value of System.currentTimeMillis() would be affected by the change of system time whereas the value of SystemClock.elapsedRealtime() would not.
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
I wanted add application signature for some application, which will be valid only for 1 or 2 days. I did enough googling but did not find enough info. So Please let me know how can i make a application get expired in 2 days..
simply add an alarm of calculating time equals to 2 days at the very first start of the app.When the alarm gets expired you will get the callback and set a global flag as false.Code in your app that if that flag is false, display a lock screen
You could save the first start time in SharedPreferences and then on each start compare the current time with the saved one. If the 2 days have passed, you can do what ever action you want.
It will probably not be possible to automatically remove the app from the device though.
If your app sends data to a server, you should store the "first run time" there. Storing it locally on the phone (for example SharedPreferences) is not very safe as it's easily overcome by changing the phone's time settings.