Sunday, July 5, 2015

android:Call method after delay and set countdown timer

You need to have Handler called every second and update the UI during every pass. When you reach 2 mins you can cancel the handler.
Code should be like this:
final Handler handler = new Handler();
//class variable
count = 0;

handler.post(new Runnable() {
    @Override
    public void run() {
        updateCounter(count++);

        if(count < 120) {
            handler.postDelayed(this, 1000);
        }
    }
});
And function to update counter:
private void updateCounter(final int count) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // you have the seconds passed
            // do what ever you want
        }
    });
}

No comments:

Post a Comment