有没有办法检测用户何时更改了设备上的时钟时间? [英] Is there a way to detect when the user has changed the clock time on their device?

查看:16
本文介绍了有没有办法检测用户何时更改了设备上的时钟时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测 Android 系统时钟何时被用户在 Android 中重置?

Is there a way to detect when the Android system clock has been reset by the user in Android?

我正在设计一个应用程序,该应用程序使用系统时间来确定用户何时在特定时间位于特定地点,并且我不想依赖当时的网络可用性.显然,因此最好知道用户何时更改了系统时钟,以免他们作弊".

I'm designing an app which uses system time to determine when a user is at a certain place at a certain time, and I don't want to rely on network availability at that point. Obviously it would therefore be good to know when the user has changed the system clock, so they can't "cheat".

推荐答案

是的,有.ACTION_TIME_CHANGED Intent 当设备时间改变了,你可以有一个方法在检测到这个 Intent 时触发.

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.

此意图自 API 级别 1 起就已存在于 Android 中,因此它应该适用于您可能需要兼容的任何平台.

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

您需要使用 BroadcastReceiver:

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>

这会让 Android 知道在检测到此类意图时触发您的接收器.

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

然而,虽然手机上的时钟不是特别准确(因为它们通常依赖于与接收到的时间信号同步),但根据我的经验,它们绝对不应该丢失超过大约 30 秒或每小时一分钟,绝对最大值,所以如果时间变化很小,你可以假设它是自动的.添加闰秒时,也可能会产生时间更改消息,尽管这些消息显然很小且很少发生.

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.

您可以使用 ConnectivityManager 来跟踪手机是否有连接,您可以基于此更改行为(即,只要有网络连接,并且时间是自动/网络时间,忽略时间更改或其他内容)但我不能找到有关丢失/恢复网络连接的任何意图,因此您可能必须使用轮询方法.

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.

这篇关于有没有办法检测用户何时更改了设备上的时钟时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆