c-an
c-an

Reputation: 4090

How to use postDelayed or sleep two times in two for-loops respectively

I'd like to count 5 seconds and start a function. The function also needs to call every 3 seconds.

Toast.makeText(this, "Countdown 5", Toast.LENGTH_SHORT).show();
Handler handler = new Handler();

for (int i = 5; i >= 0; --i) {
    final int idx = i;
    handler.postDelayed(() -> {
        Toast.makeText(MyActivity.this, "Countdown " + (idx - 1), Toast.LENGTH_SHORT).show();
        if (idx == 0) {
            int messageSize = messageItems.size();
            for (int j = 0; j < messageSize; j++) {
                final int jdx = j;
                Handler handler1 = new Handler();
                handler1.postDelayed(() -> {
                    Toast.makeText(MyActivity.this, messageItems.get(jdx), Toast.LENGTH_SHORT).show();
                }, 3000 * jdx);
            }
        }
    }, 1000 * (5-idx));
}

The inner loop part works well without the outer loop.

        int messageSize = messageItems.size();
        for (int j = 0; j < messageSize; j++) {
            final int jdx = j;
            Handler handler1 = new Handler();
            handler1.postDelayed(() -> {
                Toast.makeText(MyActivity.this, messageItems.get(jdx), Toast.LENGTH_SHORT).show();
            }, 3000 * jdx);
        }

And I'd like to add a feature like 5 seconds countdown. And then start Toast Message. And the Toast message also needs to toast every 3 seconds.

Upvotes: 1

Views: 45

Answers (1)

Ranjan
Ranjan

Reputation: 1146

Try this code. This gives fairly accurate results. Some lag is there sometimes but will not be noticeable. Tweek this to show your toasts.

    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);

Following is the output of timings -

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

Upvotes: 1

Related Questions