Search code examples
androidmultithreadingsleeprepeatperiod

Infinite calling some function after certain time


I've tried time&timertask, handler&runnables, nothing makes my function be called after certain time. Please, help me get rid of this.

when = System.currentTimeMillis();
t1 = new Timer();
tt = new TimerTask(){@Override public void run(){ systemClick();}};
t1.scheduleAtFixedRate(tt, when+interval, interval);

And this one:

final Handler myHandler = new Handler();

    Runnable runnableforadd  = new Runnable() 
    {
        public void run() 
        {
            systemClick();              
            myHandler.postDelayed(this, interval);
        }   
    };
    myHandler.postDelayed(runnableforadd, when + interval);

Both the first and the second I execute in onCreate(). systemClick() doesn't get called even for a one time in deed (I've put Toast in there). I don't understand in Threads well.

systemClick - is a function, where system performs click of my button - myButton.performClick() is called there (and other functions as well).

How can I solve this?

Thanks


Solution

  • The value in delay time should be the value that you want your method to wait for. For example if you want to call your method after waiting 1 minute then pass 60000.

    myHandler.postDelayed(runnableforadd, 60000);
    

    Currently you are adding "wait" in the delay time that is System.currentTimeMillis() which means your method will be called after approx "1338900000000" milliseconds