5

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.

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
Finder
  • 1,217
  • 5
  • 18
  • 46
  • Get one static variable and everytime when you change the value of time set the previous value to static variable and then access it anywhere you want. – InnocentKiller Apr 03 '14 at 06:53
  • Time won't be change from my app.It can be changed by any other application. Then how can I maintain the previous time (Before change time) – Finder Apr 03 '14 at 07:01

2 Answers2

2

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

Community
  • 1
  • 1
JonasOliveira
  • 694
  • 8
  • 26
  • 1
    It works, nice idea. One thing I add is to adjust the "starting time" (i.e. the one you stored on boot) by calculating the difference between old time and new time and add to it after every time change, so you will have the updated & correct time reference when you use this method again. – Andrew T. Mar 20 '15 at 07:01
  • It's a nice solution. But I have problem that TIME_SET sometimes receives before BOOT_COMPLETE. It would be nice to have some system id for particular boot session in android, but I can't find it. – Artem_Iens Jan 26 '17 at 08:01
0

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

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • Time won't be change from my app. It can be change from any other app or from settings. How can I get the previous time.. – Finder Apr 03 '14 at 06:59
  • whenever u got this receive event then just take current time millisecond then store it i think that the the last time before change – Bhanu Sharma Apr 03 '14 at 07:04
  • When I get the current time that will give the time after changed time only. It won't give the previous time (Before change time) – Finder Apr 03 '14 at 07:07
  • take a look on this http://stackoverflow.com/questions/5481386/date-and-time-change-listener-in-android – Bhanu Sharma Apr 03 '14 at 07:07
  • then i don't know dude may be u have to some R&D on this if u will get then please let me know also.:) – Bhanu Sharma Apr 03 '14 at 07:34
  • Is there any event getting when we just try to change time date setting like before save button press then may be your work will easy – Bhanu Sharma Apr 03 '14 at 07:35
  • Sure dude.. Once I get solution I will update you... and Thanks for your kindness.. – Finder Apr 03 '14 at 09:14