4

I can detect change clock time when app in foreground, background, or kill from Recent App by using android.intent.action.TIME_SET follow here.
However, if I Force Stop app in Setting->Apps I can not receive this broadcast anymore.
Currently, I want to detect user change clock time come back to my app after ForceStop so I do

long deltaTimeBeetweenCurrentTimeAndTimeSinceReboot = System.currentTimeMillis() - SystemClock.elapsedRealtime();
long oldDelta = mSharedPreference.getDeltaTimeBeetweenCurrentTimeAndTimeSinceReboot();
if(deltaTimeBeetweenCurrentTimeAndRebootTime - oldDelta > 5000){
    // clock time change
}

Idea is I saved a delta between currently time (System.currentTimeMillis()) and time since reboot (SystemClock.elapsedRealtime()). Every time I open app, I will compare oldDelta and newDelta (except the first time install). It work well in case: User Fore Stop app->Change time->come back to app.

However, there is still have 1 case that is: User Fore Stop app -> Change the clock time -> Reboot device -> Open my app. At this time I can not use the above method to check the clock time have changed because after reboot the SystemClock.elapsedRealtime() will reset. How can I detect clock time have changed in that case?
Any help or suggestion would be great appreciate.

Vivek Barai
  • 1,338
  • 13
  • 26
Linh
  • 57,942
  • 23
  • 262
  • 279
  • So basically you claim, that if user force stopped your app, then the broadcast receiver, that was registered through manifest tile, won't be triggered? – azizbekian Nov 09 '17 at 13:18
  • Is it possible when using remote time server ? – Son Tieu Nov 15 '17 at 07:10
  • @SonTieu unfortunately my app can work without internet connection so using remote server is not a optimal way – Linh Nov 15 '17 at 08:49
  • make different service app and install it in background – parik dhakan Nov 15 '17 at 09:46
  • 1
    suggest use a server to check device time setting, like http://worldclockapi.com/api/json/utc/now , (or you can provide your own api). your logic will become much simpler and more reliable. disadvantage: network needed – Josh Lin Nov 16 '17 at 08:36

2 Answers2

3

Few months ago I was in pretty the same situation. I didn't find any answer to directly solve the case, so I won't help you with it. But I can give you kind of advice:
Look at it with a different point of view.

What I did in my case, I answered myself to questions:

"Do I really want to know that user has changed the time - because I implicitly inform/present the fact to user?"

OR

"Do I want to know that user has changed the time - because I need it to invoke some actions or calculations in the application background?"

In my case I had NO-YES answers. So, for problematic case of Force-Stop + reboot I assumed that the time could had been changed and I reset my application's time configuration likewise the first app launch.

Let me know if it helps you anyway.

mariachi
  • 135
  • 10
  • Thank you for you best suggestion. Unfortunately in my case I need to know exactly when user change the time (it is a walking game with some small real award base on the time so we don't want user change the time to trick). Currently we allow user play game in background without internet, if we can not find the solution for check if user change the time definitely, maybe we will stop allow user play game in background. – Linh Nov 16 '17 at 01:14
1

It would be hard to implement without external etalon (backend is the best option). You may save time a user force to stop the app and when the app alive again compare with some predefined delta (time window). If you get a "big" difference consider the user is cheating.

You may also play with timestamps of the filesystem to define some inconsistency.

Maxim G
  • 1,479
  • 1
  • 15
  • 23