Advertisement
Guest User

CountDownTimer example

a guest
Dec 19th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. @SuppressWarnings("deprecation")
  2. public class Example extends Activity implements OnDateSetListener, OnTimeSetListener {
  3.     private Calendar future = Calendar.getInstance(Locale.getDefault());
  4.     private Calendar now = Calendar.getInstance(Locale.getDefault());
  5.     private CountDownTimer timer;
  6.     private TextView textView;
  7.  
  8.     @Override
  9.     public void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         textView = new TextView(this);
  12.         setContentView(textView);
  13.         showDialog(0);
  14.     }
  15.  
  16.     @Override
  17.     protected Dialog onCreateDialog(int id) {
  18.         if(id == 0)
  19.             return new DatePickerDialog(this, this, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
  20.         return new TimePickerDialog(this, this, now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), true);
  21.     }
  22.  
  23.     @Override
  24.     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  25.         future.set(year, monthOfYear, dayOfMonth, 0, 0, 0);
  26.         showDialog(1);
  27.     }
  28.  
  29.     @Override
  30.     protected void onDestroy() {
  31.         timer.cancel();
  32.         super.onDestroy();
  33.     }
  34.  
  35.     @Override
  36.     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  37.         future.set(future.get(Calendar.YEAR), future.get(Calendar.MONTH), future.get(Calendar.DAY_OF_MONTH), hourOfDay, minute, 0);
  38.  
  39.         now.setTimeInMillis(System.currentTimeMillis());
  40.         timer = new CountDownTimer(future.getTimeInMillis() - now.getTimeInMillis(), 1000) {
  41.             StringBuilder time = new StringBuilder();
  42.             @Override
  43.             public void onFinish() {
  44.                 textView.setText(DateUtils.formatElapsedTime(0));
  45.                 //mTextView.setText("Times Up!");
  46.             }
  47.  
  48.             @Override
  49.             public void onTick(long millisUntilFinished) {
  50.                 time.setLength(0);
  51.                 // Use days if appropriate
  52.                 if(millisUntilFinished > DateUtils.DAY_IN_MILLIS) {
  53.                     long count = millisUntilFinished / DateUtils.DAY_IN_MILLIS;
  54.                     if(count > 1)
  55.                         time.append(count).append(" days ");
  56.                     else
  57.                         time.append(count).append(" day ");
  58.  
  59.                     millisUntilFinished %= DateUtils.DAY_IN_MILLIS;
  60.                 }
  61.  
  62.                 time.append(DateUtils.formatElapsedTime(Math.round(millisUntilFinished / 1000d)));
  63.                 textView.setText(time.toString());
  64.             }
  65.         }.start();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement