android timer example – Android Timer Tutorial

      Comments Off on android timer example – Android Timer Tutorial

android timer example

android timer example

android timer example is set of sample code on how to use timer in android programming. Timer you can use in andorid if you wish to execute some task after some interval. Warning: Don’t use too much it will drain your andorid phone batter life.

Android Timer Example

final Handler h = new Handler();

//========================================
// Creation of Runnable Object
//========================================
final Runnable r = new Runnable() {
@Override
public void run() {

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() {
     h.post(r);
    }
}, 1000, 5000);

Android Timer is suggested only if you wish to display any kind of data from mobile or similar low memory required task like display date and time and so on. If you use this for fetching data from server then your application may stop working or can consume more battery life.

If you are beginners you can check out Android Beginners step by step guide and if you are expert have a look at Android Tips and Tricks.

PS:
If you like my article, Please share with your friends on Facebook, WhatsApp, Twitter, etc. Thanks in Advance.