Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

8
Having trouble updating UI every second for stopwatch app using an AsyncTask and Thread.sleep()
Post Flair (click to view more posts with a particular flair)
Post Body

This is homework, but I'm not looking for someone to do it for me. I'm just a little lost.

The goal is to create a countdown timer just using System.currentTimeMillis(). The exercise is to show how to use AsyncTask properly to avoid ANR.

We were told to use Thread.sleep to space out checks so that the system doesn't get bogged down, but of course Thread.sleep isn't exact and works in *roughly* the number of ms you enter. So sometimes the final check to see if a second has passed and if the UI should update is 1244ms, or 1100 ms. Which means on occasion my UI will skip a number entirely.

I've created an otherwise fully functional countdown timer (it starts, counts down correctly, and ends at the right time) aside from it updating the seconds left appropriately.

My onProgressUpdate simply does the math for seconds vs minutes of remaining time, and plugs those numbers into a pair of TextEdits. Nothing fancy there, and it works.

I know the error has to do with Thread.sleep not being accurate, and while I know that, I don't quite understand the logic involved to work around it.

My doInBackground below:

https://pastebin.com/pr2HYL4N

Author
Account Strength
100%
Account Age
11 years
Verified Email
Yes
Verified Flair
Yes
Total Karma
335
Link Karma
163
Comment Karma
172
Profile updated: 7 hours ago
Posts updated: 1 day ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago