0

谁能告诉我为什么在 android.developer 文档中他们说:just sleep for 30 seconds.

然后放 15 * 1000,这不是 30 秒,它只有 15 !

 Runnable mTask = new Runnable() {
                public void run() {
                    // Normally we would do some work here...  for our sample, we will
                    // just sleep for 30 seconds.
                    long endTime = System.currentTimeMillis() + 15*1000;
                    while (System.currentTimeMillis() < endTime) {
                        synchronized (mBinder) {
                            try {
                                mBinder.wait(endTime - System.currentTimeMillis());
                            } catch (Exception e) {
                            }
                        }
                    }

                    // Done with our work...  stop the service!
                    MyAlarmService.this.stopSelf();
                }
            };

这里

4

2 回答 2

1

它可能被设置为 30 秒,并且在某个地方有人厌倦了在测试和调试周期中等待那么长时间,所以他们缩短了它但从未更改过评论。

于 2012-04-29T16:54:10.027 回答
1

在记录使用描述性变量名称会更好的代码时,您会看到其中一个陷阱。我的猜测是他们在将值更改为 15 秒时忘记更新他们的评论。

就个人而言,我通常会引入一个常量变量,例如

private final static int 15_SECONDS = 15 * 1000;

或者使用

private final static int SECONDS = 1000;

并在代码中使用

long endTime = System.currentTimeMillis() + 15_SECONDS;

或分别

long endTime = System.currentTimeMillis() + (15 * SECONDS);

这样,代码的作用就很清楚了,如果我决定更改值也很清楚,因为很容易记住更改变量名。

于 2012-04-29T18:15:21.567 回答