Constantly repeat code
- By GameTheory
- Android Development
- 1 Replies
I've done something similar in the past using a Thread that refreshes every second.I'm trying to add a clock into my app and I need some code to be constantly running to get the current time. is there a simple way of making code constantly run?
Here's a sample to give you an idea.
Java:
Thread thread = new Thread() {
public void run() {
try {
while (!isInterrupted()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// Run your clock code here in a TextView
// Example:
tv.setText(""+((System.currentTimeMillis() - startTime)/1000));
}
});
Thread.sleep(1000); // This refreshes the thread once every second. Adjust as needed.
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();

