Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. public class Timer {
  2. private boolean isActivated;
  3. private boolean isStopped;
  4.  
  5. private CountDownTimer timer;
  6. private String name;
  7. private long timeToGo;
  8. Timer(){
  9. isActivated = false;
  10. isStopped = false;
  11. name = "Activate";
  12. }
  13. public void activate(long future, long interval,final TextView text,String _name){
  14. SystemClock.uptimeMillis();
  15. timeToGo = future;
  16. timer = new CountDownTimer(future, interval){
  17. public void onTick(long millisUntilFinished) {
  18. if(!isStopped){
  19. Time time = new Time(millisUntilFinished - 3 * 3600000);
  20. String textTime = "";
  21. Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
  22. calendar.setTime(time); // assigns calendar to given date
  23. int hrs = calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
  24. int min = calendar.get(Calendar.MINUTE);
  25. int sec = calendar.get(Calendar.SECOND);
  26. if(millisUntilFinished > 3600000){
  27. text.setTextSize(36);
  28. }
  29. if(hrs!=0)
  30. textTime+=String.format("%02d",hrs)+":";
  31. if(min!=0||hrs!=0)
  32. textTime+=String.format("%02d",min)+":";
  33. textTime+=String.format("%02d",sec);
  34. text.setText(textTime);
  35. }
  36. }
  37.  
  38. public void onFinish() {
  39. if(!isStopped)
  40. text.setText("DONE!");
  41. }
  42. };
  43.  
  44.  
  45. name = _name;
  46. isActivated = true;
  47. timer.start();
  48. }
  49. public boolean isActivated(){
  50. return isActivated;
  51. }
  52. public String getTime(){
  53. return "00:00";
  54. }
  55. public String getName() {
  56. return name;
  57. }
  58. public void stop() {
  59. timer.cancel();
  60. timer = null;
  61. isStopped = true;
  62. }
  63. public boolean isStopped() {
  64. return isStopped;
  65. }
  66. public long getTimer() {
  67. return timeToGo;
  68. }
  69. public void start() {
  70. isStopped = false;
  71. }
  72. }
  73.  
  74. if(timers[id].isStopped()){
  75. timers[id].start();
  76. timers[id].activate(timers[id].getTimer(), 1000, clocks[id*2], timers[id].getName());
  77. return;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement