Android Timer Example – Android Timer Tutorial

Android Timer Example – Android Timer Tutorial.

GTU Android Program 8

 

Timer is very useful if you would like to do the task at specific interval. Here the process of using Timer Task is bit length as we need to have two more objects ie. Runnable and Handler. Check out the complete example of Timer which will display Toast message on specific interval.

 

Location : res\layout

FileName: activity_main.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=”gtu.android.prog08.MainActivity” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”26dp”
android:text=”Message on Specific Interval”
android:textAppearance=”?android:attr/textAppearanceMedium” />

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”173dp”
android:text=”Developed By: Adarsh Patel”
android:textAppearance=”?android:attr/textAppearanceLarge” />

</RelativeLayout>

 

 

Location : src\package\

FileName : MainActivity.java

package gtu.android.prog08;

/*
* GTU Android Program 8 Android Timer Example Create an application that will display toast(Message) on specific interval of time.
*
*/

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

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

Timer t;

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

final Handler h = new Handler();

//========================================
// Creation of Runnable Object
//========================================
final Runnable r = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub

Toast.makeText(getApplicationContext(), “Wow..Its working”, Toast.LENGTH_SHORT).show();
}
};

//======================================
//Creation of Timer Object (Need to pass TimerTask())
//======================================
t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {

@Override
public void run() {

//use handler post event and pass runnable object
h.post(r);

}
}, 1000, 5000);

}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

Toast.makeText(getApplicationContext(), “Bye Bye”, Toast.LENGTH_SHORT).show();

// Don’t forget to cancel timer event before you exit from apps.
t.cancel();
//cleaning all time event and calling GC
t.purge();

//if you don’t want to do above task you can kill process
//android.os.Process.killProcess(android.os.Process.myPid());
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

 

Continue Reading…

 

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative