How to set Countdown time format

I was use runnable() to make countdown timer on my game

i was set int cd=30;

and this is the method for run it

final Handler mHandler = new Handler();

final Runnable mUpdateTimeTasks = new Runnable() {

    public void run() {
        countdowntext.setText(String.valueOf(cd));
        cd -=1;
        if(cd < 10) {
            countdowntext.setTextColor(Color.RED);
        }
        if(cd == 0) {
           focus.stop();                                      
        }
    };
    // run this in a method.
    mHandler.postDelayed(mUpdateTimeTasks, cd);
}

the code was work fine but the problem just the coundown timer run so fast, i just wonder how to make int cd=1 = 1 second too, so cd=30 = 30 second

can anyone help me?

Answers


i just wonder how to make int cd=1 = 1 second too, so cd=30 = 30 second

then you should use mHandler.postDelayed(mUpdateTimeTasks, 1000)

final Runnable mUpdateTimeTasks = new Runnable() {
      public void run() {
           countdowntext.setText(String.valueOf(cd));
           cd -=1;
           if(cd < 10) {
                countdowntext.setTextColor(Color.RED);
           }
           if(cd == 0) { 
                focus.stop();        
                mHandler.removaCallbacks(null);
                return;                   
           }
           mHandler.postDelayed(this, 1000)
};

this will not give exactly 30 seconds but something really close to it


In mHandler.postDelayed(mUpdateTimeTasks, cd); cd is millisecond

You should change your code from:

final Handler mHandler = new Handler();

final Runnable mUpdateTimeTasks = new Runnable() {

public void run() {
    countdowntext.setText(String.valueOf(cd));
    cd -=1;
    if(cd < 10) {
        countdowntext.setTextColor(Color.RED);
    }
    if(cd == 0) {
       focus.stop();                                      
    }
};
// run this in a method.
mHandler.postDelayed(mUpdateTimeTasks, cd);
 }

To this:

    final Handler mHandler = new Handler();

final Runnable mUpdateTimeTasks = new Runnable() {

    public void run() {
        countdowntext.setText(String.valueOf(cd));
        cd -=1;
        if(cd < 10) {
            countdowntext.setTextColor(Color.RED);
        }
        if(cd == 0) {
           focus.stop();    
           mHandler.removaCallbacks(null);  
           return();                                
        }
    };
    // run this in a method.
    mHandler.postDelayed(mUpdateTimeTasks, 1000);
    }

1000 means that after one second(1000/1000 sec) check the Runnable mUpdateTimeTasks method.


Need Your Help

Microchip: How to load more then one HEX file in MPLAB X

loading microchip pic18 mplab

I use MPLAB X (sometime MPLAB 8) and i get some project to finish. I have these HEX files: