Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @SuppressWarnings("deprecation")
- public class Example extends Activity implements OnDateSetListener, OnTimeSetListener {
- private Calendar future = Calendar.getInstance(Locale.getDefault());
- private Calendar now = Calendar.getInstance(Locale.getDefault());
- private CountDownTimer timer;
- private TextView textView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- textView = new TextView(this);
- setContentView(textView);
- showDialog(0);
- }
- @Override
- protected Dialog onCreateDialog(int id) {
- if(id == 0)
- return new DatePickerDialog(this, this, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
- return new TimePickerDialog(this, this, now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), true);
- }
- @Override
- public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
- future.set(year, monthOfYear, dayOfMonth, 0, 0, 0);
- showDialog(1);
- }
- @Override
- protected void onDestroy() {
- timer.cancel();
- super.onDestroy();
- }
- @Override
- public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
- future.set(future.get(Calendar.YEAR), future.get(Calendar.MONTH), future.get(Calendar.DAY_OF_MONTH), hourOfDay, minute, 0);
- now.setTimeInMillis(System.currentTimeMillis());
- timer = new CountDownTimer(future.getTimeInMillis() - now.getTimeInMillis(), 1000) {
- StringBuilder time = new StringBuilder();
- @Override
- public void onFinish() {
- textView.setText(DateUtils.formatElapsedTime(0));
- //mTextView.setText("Times Up!");
- }
- @Override
- public void onTick(long millisUntilFinished) {
- time.setLength(0);
- // Use days if appropriate
- if(millisUntilFinished > DateUtils.DAY_IN_MILLIS) {
- long count = millisUntilFinished / DateUtils.DAY_IN_MILLIS;
- if(count > 1)
- time.append(count).append(" days ");
- else
- time.append(count).append(" day ");
- millisUntilFinished %= DateUtils.DAY_IN_MILLIS;
- }
- time.append(DateUtils.formatElapsedTime(Math.round(millisUntilFinished / 1000d)));
- textView.setText(time.toString());
- }
- }.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement