0

At the moment I have a CountdownTimer which is running on the UI thread and is updating the interface according to onTick.

It's not always working as expected because I saw that the app can get stuck if I put it in background(press the home button) and start other CPU needy apps. I guess the best solution would be to move the CountDownTimer to a background thread. Am I wrong?

What is the best way to update the UI thread according to the CountDownTimer's tick? Using AsyncTask is probably not a good idea since it's recommended for short tasks but I found some recommendations for IntentService and LocalBroadcastManager.

What is the best solution for achieving this? Examples would be great. Thanks!

adriennoir
  • 1,329
  • 1
  • 15
  • 29

1 Answers1

0

1) Use AlarmManager and Pending intents for periodic future tasks. Check this

2) Use Handler to post a Runnable to update the UI.

new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context,"This is run on UI thread",Toast.LENTH_SHORT).show();
                }
            });
Community
  • 1
  • 1
user2486322
  • 847
  • 3
  • 13
  • 31