ngesh
ngesh

Reputation: 13501

Handler instead of Timer?

Basically this is what I am doing, instead of using a Timer I am using a Handler:

Handler h = new Handler();    
Runnable r = new Runnable(){

public void run(){
 //do something and schedule it again
 h.postDelayed(r, 10000);
}

};

And I say h.removeCallBacks(r); when I want to stop it. Is it bad?

Upvotes: 1

Views: 259

Answers (3)

Kapil Vats
Kapil Vats

Reputation: 5515

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

Upvotes: 1

nlt
nlt

Reputation: 555

Is it bad.?

It's not bad, but that method just removes pending posts of Runnable.

Upvotes: 5

user
user

Reputation: 87064

Is it bad.?

No(unless you have some problems:)). Don't forget to cancel that Runnable in the onPause method so you cancel any pending Runnable from running on a possible dead activity.

Upvotes: 3

Related Questions