Search code examples
javaandroidrunnablecountdowntimerjava-threads

CountdownTimer is not Running in Runnable


Hello Developers This question may seem foolish to some readers and it may have answered before but I have seen many answers on this forum regarding my issue but I could not understand single one of them. So please be kind and answer my question if you could.

The thing is I am trying to develop something like a game and trying to add computer or AI player against a human player.

For that I added a CountdownTimer in OnCreate method and it worked fine only once as OnCreate executes only once. But when I tried to add a while loop inside a runnable and inside that while loop I added the same CountdownTimer but it started giving me errors.

if(player2_name.equals("Computer")) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                while (player2_name.equals("Computer")) {
                  CountDownTimer computer_player;
                   ct = 5;
                   firstplayer_game_button.setClickable(false);
                   computer_player = new CountDownTimer(5000, 1000) {
                       @Override
                       public void onTick(long millisUntilFinished) {
                           ct = ct - 1;
                    //       timer_counter.setText("" + ct);
                           if (ct == 1) {
                               firstplayer_game_button.setClickable(true);
                           }
                       }

                       @Override
                       public void onFinish() {
                        //   secondplayer_game_button.setBackgroundResource(R.drawable.button_player_2);
                        //   firstplayer_game_button.setBackgroundResource(R.drawable.button_player_1);
                        //   firstplayer_game_button.setClickable(true);
                           if (player2_name.equals("Computer")) {

                               secondplayer_game_button.setClickable(false);

                           } else {
                               secondplayer_game_button.setClickable(true);
                           }
                           if (checker != 1) {
                               Click_Condition_checker(2);
                           }
                       }
                   };
                   computer_player.start();
                }
            }
        };
        Thread mythread = new Thread(r);
        mythread.start();

I know that a Runnable cannot interact with UI directly and for that we need handlers that is why I commented all the UI interfaces. But still no luck. And unfortunately I am not able to identify the errors. If Someone can help then it would be very kind for a foolish developer like me.


Solution

  • First, you don't need to use Runnable to make a CountDownTimer works. It's because CountDownTimer can directly interact with the UI if the code running inside the Activity.

    Second, you should not use while loop inside your code. Especially for the following code:

    while (player2_name.equals("Computer")) {
       CountDownTimer computer_player;
       ...
       computer_player = new CountDownTimer(5000, 1000) {}
       ...
    }
    

    which means you're creating new object until you exhausted all the device memory or until player2_name is not "Computer".

    You should using a boolean flag instead.

    I think what you need is can be achieved by restarting the CountDownTimer with something like this:

    // use a more readable variable name instead of computer_player
    final CountDownTimer cdtPlayer = new CountDownTimer(5000, 1000) {
       @Override
       public void onTick(long millisUntilFinished) {
         // do something
       }
    
       @Override
       public void onFinish() {
         // do something
    
         // restart to do all again
         cdtPlayer.start();
       }
    };
    
    cdtPlayer.start();
    

    Regarding your following statement:

    If Someone can help then it would be very kind for a foolish developer like me.

    Everybody is a foolish when starting to learn something. Don't be too hard with yourself ;)