How to Detect When the User Has Changed the Clock Time on Their Device

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

Yes, there is. The ACTION_TIME_CHANGED Intent is broadcast when the device time is changed, and you can have a method which will trigger when this Intent is detected.

This intent has been in Android since API level 1, so it should work on any platform you might need to be compatible with.

You'll need to handle the Broadcast with a BroadcastReceiver:

public class TimeChangedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
//Do whatever you need to
}

}

You'll also need to add something like this to your Manifest:

<receiver android:name=".TimeChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
</intent-filter>
</receiver>

This will let Android know to trigger your receiver when this type of intent is detected.

It appears as though this doesn't care who edits the time, but also does not trigger on automatic adjustments when you are synced with the network. If you lose network and regain it, though, this will likely fire since your time will be slightly different (assuming you are using automatic network time).

However, while the clocks on cell phones are not particularly accurate (since they generally rely on syncing with time signals they receive) in my experience they absolutely should not lose more than about 30 seconds or a minute per hour, at the absolute maximum, so if the time change is small you can perhaps assume it was automatic. Leap seconds, when they are added, will also likely produce a time change message, although these are obviously small and infrequent.

You can use ConnectivityManager to keep track of whether the phone has a connection or not and you can change the behavior based on that (i.e. as long as network connectivity is there, and the time is automatic/network time, ignore time changes or something) but I can't find any intents regarding losing/regaining network connectivity so you will probably have to use a polling method.

Android: How to detect if user changes system time without running a constant service to check time

You can save the current time in shared preference and compare it every time you sample it. If you get an older sample the user changed the time backward.

android detect user modifying device clock time

My App requires correct time

Never rely on the client for accurate time, on any platform.

Initially I use http calls to a trusted server, however there are situations when network is not available.

Then you cannot have accurate time, by definition.

Is there any other option I can use?

You can create your own firmware that prevents the user from setting the time.

Can I rely on broadcast ACTION_TIME_CHANGED to detect that the user modofied the device clock?

No, insofar as there are several possible reasons why that broadcast will be sent.

Does my app always need to be running in order to get this notification (this I would like to avoid)?

I can tell you that the only receivers of ACTION_TIME_CHANGED that I see in the Android source code are ones that register for it via registerReceiver(), not the manifest. This broadcast does not fit the usual pattern where registerReceiver() is required, though. It is probably simpler for you to just try it.



Related Topics



Leave a reply



Submit