Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

1 Answer 1

up vote 1 down vote accepted

You can use a application class http://developer.android.com/reference/android/app/Application.html


share|improve this answer
Thank you very much this helped a lot! :) –  deucalion0 Apr 13 '13 at 11:58

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.