1

I want to be able to calculate remaining time of myHandler2.postDelayed(). I was using this answer but it returns wrong value. Here is my code where the startTime variable is:

public void attackOnChibi(ChibiCharacter cc, boolean able) {

    runnable = () -> {
        Log.i("a", "a");
    };

    if (able) {
        startTime = System.nanoTime();
        myHandler2.postDelayed(runnable, count += 2000);
    }
    if (!able) {
        myHandler2.removeCallbacksAndMessages(null);
        count = 0;
    }
}

And the code when I calculates and displays this remaining time:

        elapsedTime = System.nanoTime() - gs.enemies.get(gs.enemyId - 1).startTime;
        remainingTime = gs.enemies.get(gs.enemyId - 1).count + 2000 - TimeUnit.MILLISECONDS.convert(elapsedTime, TimeUnit.NANOSECONDS);
        Log.i("elapsed/remaining", elapsedTime + " " + remainingTime);

Why it does not work? Help me please...

Qbi
  • 445
  • 4
  • 13

0 Answers0