Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. public class UIUpdater {
  2.  
  3. // Create a Handler that uses the Main Looper to run in
  4. private Handler mHandler = new Handler(Looper.getMainLooper());
  5.  
  6. private Runnable mStatusChecker;
  7. private int UPDATE_INTERVAL = 2000;
  8.  
  9. /**
  10. * Creates an UIUpdater object, that can be used to
  11. * perform UIUpdates on a specified time interval.
  12. *
  13. * @param uiUpdater A runnable containing the update routine.
  14. */
  15. public UIUpdater(final Runnable uiUpdater) {
  16. mStatusChecker = new Runnable() {
  17. @Override
  18. public void run() {
  19. // Run the passed runnable
  20. uiUpdater.run();
  21. // Re-run it after the update interval
  22. mHandler.postDelayed(this, UPDATE_INTERVAL);
  23. }
  24. };
  25. }
  26.  
  27. /**
  28. * The same as the default constructor, but specifying the
  29. * intended update interval.
  30. *
  31. * @param uiUpdater A runnable containing the update routine.
  32. * @param interval The interval over which the routine
  33. * should run (milliseconds).
  34. */
  35. public UIUpdater(Runnable uiUpdater, int interval){
  36. this(uiUpdater);
  37. UPDATE_INTERVAL = interval;
  38. }
  39.  
  40. /**
  41. * Starts the periodical update routine (mStatusChecker
  42. * adds the callback to the handler).
  43. */
  44. public synchronized void startUpdates(){
  45. mStatusChecker.run();
  46. }
  47.  
  48. /**
  49. * Stops the periodical update routine from running,
  50. * by removing the callback.
  51. */
  52. public synchronized void stopUpdates(){
  53. mHandler.removeCallbacks(mStatusChecker);
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement