Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to create a class which allows me to do something every 5 seconds and I want to be able to start and stop this from running from any of the other classes. Here is my Timer class:

public class Timer extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub


    Handler handler = new Handler();
    Runnable runnable = new Runnable() {

        public void run() {

            Toast.makeText(getApplicationContext(), "DISPLAY MESSAGE", Toast.LENGTH_SHORT).show();
            handler.postDelayed(runnable, 5000);

Now my knowledge on Object Orientated programming is not as good as I thought it was, I am used to creating instances of classes and using the classes properties, but how can I use a classes properties once across the whole program, do I still create instances of the Timer class in other classes? Should I use the Singleton pattern, or should I make the class static?

So basically In Class1 I would like to start the timer by doing this:;

And perhaps in Class2 stop the runnable started in Class1 by doing this:


I am looking for advice and an explanation on the correct way to program this idea and why.

Thank you!

share|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.