Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to make a countdown timer for a game/date in android. I want to create a timer that displays the days, hours, minutes, and seconds to a date I specify with a final variable. The timer then sets text views to show the days, hours, minutes, and seconds to the user.

Any suggestions about how I could code this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
463 views
Welcome To Ask or Share your Answers For Others

1 Answer

CountDownTimer that will display the time formatted to hours,minute,days,and seconds.

 public class DemotimerActivity extends Activity {
        /** Called when the activity is first created. */
         TextView tv;
         long diff;
         long oldLong;
         long NewLong;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            tv = new TextView(this);
            this.setContentView(tv);
            SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
            String oldTime = "19.02.2018, 12:00";//Timer date 1
            String NewTime = "20.02.2018, 14:00";//Timer date 2
            Date oldDate, newDate;
            try {
                oldDate = formatter.parse(oldTime);
                newDate = formatter.parse(NewTime);
                oldLong = oldDate.getTime();
                NewLong = newDate.getTime();
                diff = NewLong - oldLong;
           } catch (ParseException e) {
                e.printStackTrace();
       }
         MyCount counter = new MyCount(diff, 1000);
         counter.start();
    }


    // countdowntimer is an abstract class, so extend it and fill in methods
    public class MyCount extends CountDownTimer {
    MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        txtNumber1.setText("done!");
    }

    @Override
    public void onTick(long millisUntilFinished) {
         long millis = millisUntilFinished;
        String hms = (TimeUnit.MILLISECONDS.toDays(millis)) + "Day "
                + (TimeUnit.MILLISECONDS.toHours(millis) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millis)) + ":")
                + (TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)) + ":"
                + (TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
        txtNumber1.setText(/*context.getString(R.string.ends_in) + " " +*/ hms);
    }
}

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...