0

I am implementing a countdown timer inside an activity to make periodic (5 seconds) requests for a twitter feed. Is this a good approach? I know it will work, but just wondering if there are any better approaches? or known gotchas with this one.

Code Complete
  • 43
  • 1
  • 5

2 Answers2

0

Seems fine. You can also try another approach:

Load the feed once and then let the user refresh it manually using a pull-to-refresh kind of feature like twitter does (See How to implement Android Pull-to-Refresh). Saves data and gives the user more control as well.

Community
  • 1
  • 1
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
0

Your approach will work. I highly recommend passing the same value to both parameters, for example new CountDownTimer(5000, 5000);. But it's more than you need: a CountDownTimer is a Handler with a few extra features. You aren't using them though, so you should just use a Handler.

Every view already has a Handler, all you need is a Runnable:

ListView listView;
Runnable everyFiveSeconds = new Runnable() {
    @Override
    public void run() {
        // Call your background thread to refresh the data 
        listView.postDelayed(this, 5000);
    }
}

And in onCreate() call:

listView.post(everyFiveSeconds);
Sam
  • 86,580
  • 20
  • 181
  • 179