-1

I am referring to the following url in order to achieve my functionality.

Is there a way to detect when the user has changed the clock time on their device?

I want this functionality for the offline mode within my application. But it seems that even if I revert back to the original time, it triggers the broadcast.

Is there any fix for the same?

Community
  • 1
  • 1
android developer
  • 1,253
  • 2
  • 13
  • 43

1 Answers1

0

One way to fix this simply is to debounce it. If you change the time, you know in the next few hundred milliseconds you're going to get another broadcast due to it. So have a static boolean in your receiver that defaults to false. When you change the time yourself, set it to true. When you receive a broadcast, check the flag. If its false, change the time back. If its true, set the flag to false and ignore this broadcast.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • When you change the time yourself, set it to true. - this will happen when the broadcast is received – android developer Jun 09 '14 at 06:58
  • No, every other time. Because you check the flag before setting it yourself, and if its true you don't set it- you just make the flag false. Thus skipping anything that came from this app. – Gabe Sechan Jun 09 '14 at 07:15
  • I think I am getting your solution the other way. When I goto settings->date time and change the time, a broadcast is triggered with a Toast saying "the device time is tampered". Now, the toast is pretty much in the receiver. This is correct. Now, I press the home button and goto the settings. Change it back to the original time. Again the broadcast is triggered which should be stopped. Because now the time is correct – android developer Jun 09 '14 at 07:20
  • 1
    Ah, ok. I thought you meant your broadcast receiver itself was changing it back, not the user. That's a little more difficult then- you need to know what the correct time is to see if the user changed it back or not. Which requires you to keep track of what it is from startup and remember what the time should be. Then when you get a broadcast, you get the current time and see if it was changed to (approximately) the time you expect. If so you ignore it, if not you launch the toast. – Gabe Sechan Jun 09 '14 at 08:06