如何避免安卓游戏时钟漏洞? [英] How can you avoid clock exploits in Android games?

查看:122
本文介绍了如何避免安卓游戏时钟漏洞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测量在一段时间内,可以持续长达几个小时。我假设正常的方式做,这会是这样的:

I need to measure a period of time that can last up to a few hours. I'm assuming the normal way to do this would be something like:

Date date = new Date();
...wait some time...
(new Date()).getTime() - date.getTime())

但可以用户改变Android的时钟拨回一小时,以欺骗游戏,使时间跨度更短?将从在线源读取时间是最好的解决办法?

But could a user change Android's clock back an hour to cheat the game and make the timespan shorter? Would reading time from an online source be the best solution?

推荐答案

新的Date()使用 System.currentTimeMillis的()其中取决于OS时钟 - 且受当用户改变系统日期和时间来改变。

new Date() uses System.currentTimeMillis() which depends on the OS clock - and is subject to change when the user changes the system date and time.

您应该使用 System.nanoTime(),它具有以下特点:

You should use System.nanoTime(), which has following properties:

/**
 * Returns the current value of the most precise available system
 * timer, in nanoseconds.
 *
 * <p>This method can only be used to measure elapsed time and is
 * not related to any other notion of system or wall-clock time.
 * The value returned represents nanoseconds since some fixed but
 * arbitrary time (perhaps in the future, so values may be
 * negative).  This method provides nanosecond precision, but not
 * necessarily nanosecond accuracy. No guarantees are made about
 * how frequently values change. Differences in successive calls
 * that span greater than approximately 292 years (2<sup>63</sup>
 * nanoseconds) will not accurately compute elapsed time due to
 * numerical overflow.
 *
 * <p> For example, to measure how long some code takes to execute:
 * <pre>
 *   long startTime = System.nanoTime();
 *   // ... the code being measured ...
 *   long estimatedTime = System.nanoTime() - startTime;
 * </pre>
 * 
 * @return The current value of the system timer, in nanoseconds.
 * @since 1.5
 */
public static native long nanoTime();

这不能由仅改变系统日期操纵

This can not be manipulated by just changing the system date.

这篇关于如何避免安卓游戏时钟漏洞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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