Timer

We can use java.util.Timer in Android for timer task.

Let’s start!

Step

  • Create Task which are implemented TimerTask(This is timer operation)
  • Create Timer instance
  • Invoke schedule method

That’s all.

Thread

Timer works as new Thread.
We have two types of Timer Thread

  • Daemon
  • User

In Daemon thread mode, Main thread doesn’t wait daemon threads.
If the program finishes, daemon thread finish automatically.
In User thread, the situation is opposite. Wait until other thread was gone.
We can set these mode when creating Timer instance.

Timer

Timer timer = new Timer(true);  // true is Daemon thread

How to use timer

timer = new Timer(true);
CountDownTimerTask task = new CountDownTimerTask();
task.setListener(this);
timer.schedule(task,100, 100); // delay, period

CountDownTimerTask is class which are implemented TimerTask
TimerTask is actual operation of timer operation.
To communicate with Activity, I recommend to use Listener(interface)

Let’s see example.

Sample

This is simple Timer. It plays count down timer.
TimerTask run method does calling listener method.
Be careful, if you want to change UI, you need to post method because Timer thread is different from UI thread.

Results

timer

TimerTask

TimerListener is listener to connect Activity.

public class CountDownTimerTask extends TimerTask 
{
	private Handler handler;
	
	private long start;
	
	private long current;
	
	TimerListener listener;
	
	public CountDownTimerTask()
	{
		handler = new Handler();
		this.start = System.currentTimeMillis();
	}
	
	public void setListener ( TimerListener listner )
	{
		this.listener = listner;
	}
	
	
	@Override
	public void run() 
	{
		this.current = System.currentTimeMillis();
		final long time = (this.current - this.start) / 1000;
		
		handler.post(new Runnable()
		{
			@Override
			public void run() 
			{
				if ( listener != null )
				{
					listener.processTimer(time);
				}
			}
		});
	}
}

Keep start time. And every time task method calls, get current time and get difference.

Listener

Listener is a kind of techniques to communicate other class for Java.
In this case, I used it to connect Activity.

public interface TimerListener 
{
	public void processTimer( long time );
}

View

public class TimeView extends View 
{
	Paint paint;
	
	private int screenWidth;        // width
	
	private int screenHeight;       // height
	
	private long time;
	
	public TimeView(Context context, long time) 
	{
		super(context);
		this.time = time;
		paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	}

	@Override
	protected void onSizeChanged ( int w, int h, int oldw, int oldh )
	{
		super.onSizeChanged(w, h, oldw, oldh);
		this.screenWidth = w;
		this.screenHeight = h;
	}
	
	@Override
	protected void onDraw ( Canvas canvas )
	{
		paint.setColor(Color.WHITE);
		paint.setTextSize(30);
		paint.setFakeBoldText(true);
		canvas.drawText(String.format("%d", time), this.screenWidth - 30 * 3, 30, paint);
	}
	
	public void setTime ( long time )
	{
		this.time = time;
	}
}

Test(Activity)


public class TimerActivity extends Activity implements TimerListener
{
TimeView timeView;

long start;

Timer timer;

CountDownTimerTask task;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

this.start = 500;

timeView = new TimeView(this, this.start);
addContentView(timeView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

timer = new Timer(true);
task = new CountDownTimerTask();
task.setListener(this);
timer.schedule(task,100, 100); // delay, period
}

@Override
public void processTimer(long time)
{
long current = this.start – time;
timeView.setTime(current);
timeView.invalidate();

if ( current <= 0 ) { timer.cancel(); timer = null; } } @Override protected void onDestroy() { super.onDestroy(); if ( timer != null ) { timer.cancel(); timer = null; } } } [/java]

Reference

Android Developer