Java – Detects user changes to clock time after forcing an application to stop

Detects user changes to clock time after forcing an application to stop… here is a solution to the problem.

Detects user changes to clock time after forcing an application to stop

I can detect changing the clock time by using android.intent.action.TIME_SET when the app is in the foreground, in the background, or terminated from the Recent App, please follow here .
However, if I do the Force Stop app in Setting->Apps, I will no longer be able to receive this broadcast.
Currently, I wanted to detect that the user changed the clock time back to my app after ForceStop, so I did so

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

The idea is to save an increment between the current time (System.currentTimeMillis()) and the time after the reboot (SystemClock.elapsedRealtime()). Every time I open the app, I compare oldDelta and newDelta (except for the first install). It is suitable for the following cases: user Fore Stop app->Change time->come back to app

However, there is still 1 case: user Fore Stop app -> change clock time -> restart device -> open my app. At this point I can’t use the above method to check if the clock time has changed, because after reboot SystemClock.elapsedRealtime() will be reset. In this case, how can I detect if the clock time has changed?
Any help or advice would be appreciated.

Solution

I had the same situation a few months ago. I didn’t find any direct answer to solve the case, so I won’t help you. But I can give you some advice:
Look at it from a different angle.

What did I do in my case, I answered my own question :

“Do I really want to know that the user has changed the time – because I implicitly informed/showed this fact to the user?”

or

“Do I want to know that the user changed the time – because I need it to invoke some operation or calculation in the background of the application?”

In my case, my answer is “no” or “is”. So for the Force-Stop + reboot case in question, I assume the time may have changed and I reset my app’s time configuration as if I were launching the app for the first time.

Please let me know if it helps you.

Related Problems and Solutions