Skip to content

Instantly share code, notes, and snippets.

@Bahaaib
Created July 17, 2016 11:53
Show Gist options
  • Select an option

  • Save Bahaaib/f48d5f482baee7b6479e24bc2fbc219e to your computer and use it in GitHub Desktop.

Select an option

Save Bahaaib/f48d5f482baee7b6479e24bc2fbc219e to your computer and use it in GitHub Desktop.
How to initialize an Android timer code
final Handler handler = new Handler(); // A handler is a way used to run the code every certain delayed time
Runnable run = new Runnable (){ // A Runnable is the that code
@Override
public void run (){ // An override fuction describe exactly the action to be taken within that time
//Insert a code to be run every certain "delayed time"
handler.postDelayed (this, 1000); // initialize the handler delayer for 1000msec= 1 second
}
handler.post(run); // handler initializer
}
//////////////////////////////////////////////////////////////////////////
//////////////////// Another Timer Method////////////////////////////////
////////////////////////////////////////////////////////////////////////
new CountDownTimer = (10000, 1000){ // to take the initial time and the frequency of decreasing
public void onTick(long milliSecondsUntilDone){
//count down running
}
public void onFinish (){
//what to do after countdown finishes
}
}.start();
/* The countdown method is preferable cause it's being deconstructed immediately from memory right after its
job is done unlike the handlers which are stuck in the memory. it makes the App more slow*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment