I want to update UI every 100ms. After searching in StackOverflow, I found a solution using Runnable
and Handler
like this
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
//update UI here
handler.postDelayed(this, 100);
}
};
runnable.run();
It works! But I have some questions:
- Which thread does this
Runnable
run on? MainThread or another thread? Here is the docs aboutpostDelay
Handler
is attached MainThread, so is Runnable
running on MainThread?
- If
Runnable
is running on MainThread, why needsHandler
? According to my knowledge,Handler
is used to send messages between two threads