有没有一种方法来检测,当用户更改的时钟时间的设备上?方法来、时钟、时间、用户

2023-09-12 02:10:53 作者:伤口撒把盐

有没有一种方法来检测,当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当设备时间改变意图是广播,并可以设置当检测到这种意图,这将触发一个方法。

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.