simple timer in Android

12,175

Solution 1

You should only need one Handler and one Timer. Try moving those out of the loop:

final Handler handler = new Handler(); 
final Timer timer = new Timer();
final Runnable doA = new Runnable() {
    @Override
    public void run() {
        a();
    }
};

public void RRryan(){
    for (int i = 1; i <= 5; i++) {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                handler.post(doA);
            }
        };
        timer.schedule(task, i * 1000); 
    }
}

The schedule delays are not incremental, so make sure you actually set them to different values, otherwise they will all fire at once.

Solution 2

I could propose my own class for showing some clocks and dates in different views.

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.os.Handler;
import android.widget.TextView;
/**
 * The class for creating and refreshing many different fields on different layouts,
 * that can hold actual time and/or date in different formats 
 * The formats should be as in http://developer.android.com/reference/java/text/SimpleDateFormat.html. 
 * Only present and visible fields are being actualized, so there is no need to clean the clock list after closing an activity
 * 
 * Examples of use:
 * 
 *      Clock.registerClock((TextView) findViewById(R.id.TimeField), "HH:mm");
 *      Clock.registerClock((TextView) findViewById(R.id.DateField), "d.M.yyyy EEE");
 *      Clock.start(10000L);
 *
 * @author Petr Gangnus
 */
public final class Clock {
    /**
     * the handler that works instead of timer and supports UI
     */
    static private Handler handler = new Handler();
    /**
     * the interval of the time refreshing
     */
    static private long refreshStep;

    /**
     * pairs TextView timer+time/date format
     */
    private TextView clockFace;
    private String format;
    private Clock(TextView clockFace, String format){
        this.clockFace=clockFace;
        this.format=format;
    }
    // here is the list of views containing the visual timers that should be held actual
    static private ArrayList<Clock> clocks=new ArrayList<Clock>();
    /**
     * fills all timer fields by actual time value, according to their formats.
     */
    static private Runnable mUpdateTimeTask = new Runnable() {
       public void run() {
           for(Clock clock:clocks){
               showActualTimeDate(clock);
           }
           handler.postDelayed(this,refreshStep);
       }
    };

    //============================================ public members ====================================================================
    /**
     * add a clock to the list of updating clocks
     * @param clockFace - the place where the time or date will be shown 
     * @param format - the format of the time/date 
     * @return
     */
    public static boolean registerClock(TextView clockFace, String format){
        if (clockFace==null) return false;
        if(clocks.contains(clockFace)){
            // old clockFace
            clocks.get(clocks.indexOf(clockFace)).format=format;
        } else {
            // new clockFace
            clocks.add(new Clock(clockFace, format));
        }
        return true;
    }
    /**
     * remove a clock from the updating list
     * @param clockFace
     * @return
     */
    public static boolean unRegisterClock(TextView clockFace){
        if (clockFace==null) return false;
        if(clocks.contains(clockFace)){
            // found clockFace
            clocks.remove(clocks.indexOf(clockFace));
        } else {
            // not found clockFace
            return false;
        }
        return true;
    }
    /**
     * put in the "place" the actual date/time in the appropriate "format"
     * @param place
     * @param format
     */
    public static void showActualTimeDate(Clock clock){
        if (clock.clockFace==null) return;
        if (clock.clockFace.getVisibility()!=TextView.VISIBLE) return;
        Date thisDate=new Date();
        SimpleDateFormat df=new SimpleDateFormat(clock.format);
        clock.clockFace.setText(df.format(thisDate));
    }
    /**
     * start the ticking for all clocks
     * @param step the tick interval
     */
    public static void start(long step) { 
        refreshStep=step;
        handler.removeCallbacks(mUpdateTimeTask);
        handler.postDelayed(mUpdateTimeTask, 0);
    }
    /**
     * Stopping ticking all clocks (not removing them)
     * the calling could be put somewhere in onStop
     */
    public static void stop() { 
        handler.removeCallbacks(mUpdateTimeTask);
    }
}
Share:
12,175
Ryan
Author by

Ryan

Updated on June 04, 2022

Comments

  • Ryan
    Ryan almost 2 years

    I am a complete noob and have created this simple timer:

     public void RRryan(){
    
         for (int i=0;i<5;i++){
    
             final Handler handler = new Handler(); 
             Timer t = new Timer(); 
             t.schedule(new TimerTask() { 
                     public void run() { 
                             handler.post(new Runnable() { 
                                     public void run() { 
             a();
    
                                     } 
                             }); 
                     } 
             }, 1000); 
    
         }
     }
    

    The function a() that it calls basically updates a text view with a ++1. But the above does not work :(

    Instead of going slowly every second, it just updates it after 5 secs.

    What am I doing wrong?

    Thanks! Ryan

  • Ryan
    Ryan about 13 years
    What do you mean by this: "The schedule delays are not incremental, so make sure you actually set them to different values, otherwise they will all fire at once." and I am getting this run time exception when running my program: " timertask is scheduled already"
  • Matthew Willis
    Matthew Willis about 13 years
    Ah okay, then you need to actually create new tasks in your loop. What I meant was that you need to schedule the tasks at 1000, 2000, 3000, etc. and not all at 1000.