Advertisement
Ankhwatcher

Update Time

Oct 13th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. private Runnable mUpdate = new Runnable() {
  2.         public void run() {
  3.             boolean firstRun = true;
  4.             while (true) {
  5.                 SharedPreferences settings = getSharedPreferences(WindowActivity.PREFS_NAME, 0);
  6.                 Calendar newCal = Calendar.getInstance();
  7.  
  8.                 int minutes = newCal.get(Calendar.MINUTE);
  9.                 int seconds = newCal.get(Calendar.SECOND);
  10.                 int hours = 0;
  11.                 if (!settings.getBoolean(h_mode, false)) {
  12.                     hours = newCal.get(Calendar.HOUR_OF_DAY);
  13.                     timeString = String.format("%02d", hours) + ":" + String.format("%02d", minutes);
  14.                 } else {
  15.                     hours = newCal.get(Calendar.HOUR);
  16.                     switch (newCal.get(Calendar.AM_PM)) {
  17.                     case 0:
  18.                         timeString = String.format("%02d", hours) + ":" + String.format("%02d", minutes) + "am";
  19.                     case 1:
  20.                         timeString = String.format("%02d", hours) + ":" + String.format("%02d", minutes) + "pm";
  21.                     }
  22.                     ;
  23.                 }
  24.  
  25.                 if (firstRun || seconds == 58 || seconds == 59 || seconds == 00) {
  26.                     firstRun = false;
  27.                     runOnUiThread(new Runnable() {
  28.                         @Override
  29.                         public void run() {
  30.                             timeText.setText(timeString);
  31.                         }
  32.                     });
  33.                 } else {
  34.                     synchronized (this) {
  35.                         try {
  36.                             Thread.sleep(58000 - seconds * 1000);
  37.                         } catch (InterruptedException e) {
  38.                             e.printStackTrace();
  39.                         }
  40.                     }
  41.                 }
  42.             }
  43.         }
  44.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement