1 回答
TA贡献1794条经验 获得超7个赞
试试这个代码。这给出了相当准确的结果。有时会有一些滞后,但不会明显。Tweek 这个来展示你的祝酒词。
int counter = 5;
Handler handler = new Handler();
ArrayList<String> messageItems = new ArrayList<>();
messageItems.add("Message 1");
messageItems.add("Message 2");
messageItems.add("Message 3");
Runnable runnable = new Runnable() {
@Override
public void run() {
counter--;
if (counter > 0) {
Log.d("HOME", "onCreate: countdown " + counter + " " + System.currentTimeMillis());
handler.postDelayed(this, 1000);
}
if (counter == 1) {
for (int j = 0; j < messageItems.size(); j++) {
final int jdx = j;
Handler handler1 = new Handler();
handler1.postDelayed(() -> {
Log.d("HOME", "onCreate: countdown " + messageItems.get(jdx) + " " + System.currentTimeMillis());
}, 3000 * jdx);
}
}
}
};
Log.d("HOME", "onCreate: countdown 5 " + System.currentTimeMillis());
handler.postDelayed(runnable, 1000);
以下是计时的输出 -
Countdown 5 1551720212370
+1004
Countdown 4 1551720213374
+1019
Countdown 3 1551720214393
+1013
Countdown 2 1551720215406
+1568
Countdown 1 1551720216974
+413
Message 1 1551720217387
+2589
Message 2 1551720219976
+2999
Message 3 1551720222975
添加回答
举报