I need stuff to be done every 200 milliseconds and I need it to be smooth and even. The task to do is medium-weight(in both cases, of the question 1 and 2).
Searching for timers look like the best solution from the community is to use an handler, a runnable and postDelayed()
, ( How to set timer in android? ).
Updated Questions:
-Which way is most precise and fast if I have to interact with the UI, postDelayed()
or schedule()/scheduleAtFixedRate()
?
-Which way is most precise and fast if I don't have to interact with the UI, postDelayed()
or schedule()/scheduleAtFixedRate()
?
-Both ways could be executed on the main process or on a separate one? how?
This way I have the mainclass local objects, but is it a new thread? Is it a good practice?
timer.schedule( new TimerTask() {
@Override
public void run() {
textview.setText(str);
//other stuff
}
}, 0, 200);
The next is another with schedule, the task is in a separate task, not the timer itself, I guess, and I can't interact with the UI directly, I guess...
timer.schedule( new aclass(), 0, 200);
Handler way(from Dave.B):
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
@Override
public void run() {
TextView.setText(str);
// other stuff
timerHandler.postDelayed(this, 500); }
};
@Override
public void onCreate(Bundle savedInstanceState) {
...
timerHandler.postDelayed(timerRunnable, 0);
}