I tried the Handler solution myself, especially after reading this article on the resource pages: http://developer.android.com/resources/articles/timed-ui-updates.html . But I wanted to be able to start and stop the timer in the same way as a stopwatch, and after the timer resumes, the Handler solution starts updating much less often - on the emulator every five seconds. In the end, I found a review on a blog that suggested a solution with an inner class extending AsyncTask. You can use the publishProgress () - onProgressUpdate () function in AsyncTask. From onProgressUpdate (), you can make changes βdirectlyβ to the user interface stream, for example. myTextView.setText (...). This publishes the results much more often. Here's a super simple implementation of this inner class:
private class UpdateTimerLabel extends AsyncTask<Integer, Integer, Integer> { @Override protected Integer doInBackground(Integer... arg0) { while (true) { try { Thread.sleep(1000); publishProgress(arg0); Log.d(tag, "AsyncTask tries to publish"); } catch (InterruptedException e) { e.printStackTrace(); }if (1 == 0) break;
source share