Timer in Android

Timers will schedule and execute the tasks(one-shot or recurring).Each timer has one thread,where the tasks executes sequentially. When this thread is busy running a task, runnable tasks may be subject to delays.

One-shot are scheduled with either a absolute time or after a relative delay.
Recurring tasks are scheduled to run at an fixed period or a fixed rate.

Fixed-period execution:
It Runs based on the start time of the previous run.

Fixed-rate execution:
It doesn't based on the start time of the previous run.

cancel() is used to stop the Timer. 

Create Android Project with name as "TimerDemo" with package name  "com.android.timerdemo". Create Activity with name as "TimerDemo" and it will start the Timers. This is the code for TimerDemo.

package com.android.timerdemo;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class TimerDemo extends Activity {
Button single,multiple;
public long single_delay = 5000l;
public long multiple_delay = 10000l;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer_demo);
        single = (Button)findViewById(R.id.one_shot_btn);
        multiple = (Button)findViewById(R.id.recurring_tasks_btn);
        single.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Timer single_timer = new Timer();
single_timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(TimerDemo.this, "One-shot Timer", Toast.LENGTH_LONG).show();
}
});
}
}, single_delay);
}
});
        multiple.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Timer multiple_timer = new Timer();
multiple_timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(TimerDemo.this, "Recurring Timer", Toast.LENGTH_LONG).show();
}
});
}
},0, multiple_delay);
}
});
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.timer_demo, menu);
        return true;
    }
    
}

Paste below code to activity_timer_demo.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".TimerDemo" >
    
    <Button
        android:id="@+id/one_shot_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/recurring_tasks_btn"
        android:layout_alignParentTop="true"
        android:layout_marginTop="85dp"
        android:text="@string/one_shot" />

    <Button
        android:id="@+id/recurring_tasks_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/one_shot_btn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="132dp"
        android:text="@string/recurring_shot" />


</RelativeLayout>

Paste below code to strings.xml

<string name="one_shot">Single Task</string>
<string name="recurring_shot">Recurring Task</string>

you can download source code here.

Comments

Popular posts from this blog

SQLiteDatabase With Multiple Tables

Programmatically turn ON/OFF WiFi on Android device

Android Service and IntentService