【问题标题】:What is SystemClock.elapsedRealtime - millisecond?什么是 SystemClock.elapsedRealtime - 毫秒?
【发布时间】:2019-06-08 04:45:01
【问题描述】:

我对我从给出的一些答案中检索到的一段代码有一些疑问,该答案用于避免用户多次单击同一个按钮。有人可以向我解释这部分代码的作用和示例吗?

代码是

private long mLastClickTime = 0;

//Avoid the user clicking more than one
                if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
                    return;
                }
                mLastClickTime = SystemClock.elapsedRealtime();

if 条件放在每个 button.setonclicklistener 下方

我只是想了解这部分代码只做了什么:)

【问题讨论】:

    标签: android


    【解决方案1】:

    我会用更详细的变量名来解释它。

    private long mLastClickTime = 0;
    private long theUserCannotClickTime = 1000; // milliseconds. the time that must pass before the user's click become valid
    
    
    long currentTime = SystemClock.elapsedRealtime(); // not necessarily the current realworld time, and it doesn't matter. You can even use System.currentTimeMillis()
    long elapsedTime = currentTime - mLastClickTime; // the time that passed after the last time the user clicked the button
    
    if (elapsedTime < theUserCannotClickTime)
        return; // 1000 milliseconds hasn't passed yet. ignore the click
    }
    // over 1000 milliseconds has passed. do something with the click
    
    // record the time the user last clicked the button validly
    mLastClickTime = currentTime;
    
    

    【讨论】:

      【解决方案2】:

      elapsedRealtime() 和 elapsedRealtimeNanos() 返回系统启动后的时间,包括深度睡眠。这个时钟保证是单调的,即使在 CPU 处于省电模式时也会继续计时,因此是通用间隔计时的推荐依据。

      为了进一步检查这个方法

      https://developer.android.com/reference/android/os/SystemClock.html#elapsedRealtime()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-24
        • 1970-01-01
        • 2013-01-03
        • 1970-01-01
        • 2018-02-17
        • 1970-01-01
        • 2011-02-04
        相关资源
        最近更新 更多