I am trying to make a clock, using a TextView
:)
Someone here told me that I couldn't use normal threads to change the UI, but Handler
or AsyncTask
. I managed to get it working a few days ago, but was not a consistent thread.
Now what I want is a consistent thread that is always changing the text of my Textview
. I tried using this, but didn't work, any help?
private void startClock() {
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
while (true) {
final long millis = System.currentTimeMillis() - MainActivity.startedAt;
clock.setText("" + millis);
runOnUiThread (new Runnable() {
@Override
public void run() {
clock.setText("" + millis);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}, 2000);
}