Here another nice topic am cover Countdown Timer , Here Is the Android Official Documentation is http://developer.android.com/reference/android/os/CountDownTimer.html ,
In this topic am fount two types of count down timers.
FIRST ONE:
SECOND ONE:
In this topic am fount two types of count down timers.
FIRST ONE:
- String oldFormat = "yyyy-MM-dd HH:mm:ss";SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);TimeZone obj = TimeZone.getTimeZone("CST");sdf1.setTimeZone(obj);Date date = sdf1.parse(" your match time ");millis = date.getTime();long currentTimeInMili = new Date().getTime(); // Current timefinal CountDownTimer Counter1 = new CountDownTimer(millis - currentTimeInMili, 1 * 1000) {public void onTick(long millisUntilFinished) {matchUpcomingText.setText("" + formatTime(millisUntilFinished) + "till match start");}public void onFinish() {matchUpcomingText.setText("Finished!");}}.start();
SECOND ONE:
- String oldFormat = "yyyy-MM-dd HH:mm:ss";SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);TimeZone obj = TimeZone.getTimeZone("CST");sdf1.setTimeZone(obj);Date date = sdf1.parse(" your match time ");millis = date.getTime();long currentTimeInMili = new Date().getTime();MyCount counter = new MyCount(millis - currentTimeInMili, 1 * 1000);counter.start();public static class MyCount extends CountDownTimer {public MyCount(long millisInFuture, long countDownInterval) {super(millisInFuture, countDownInterval);}// MyCountpublic void onPause() {onPause();}// finishpublic void onTick(long millisUntilFinished) {matchUpcomingText.setText(""+ formatTime(millisUntilFinished)+ " till match start");Log.i(" formatTime ==> ", "" + formatTime(millisUntilFinished));Log.i(" millisUntilFinished ==> ", "" + millisUntilFinished);}// on tick@Overridepublic void onFinish() {//onStop();}// finish}public static String formatTime(long millis) {String output = "00:00";try {long seconds = millis / 1000;long minutes = seconds / 60;long hours = seconds / 3600;long days = seconds / (3600 * 24);seconds = seconds % 60;minutes = minutes % 60;hours = hours % 24;days = days % 30;String sec = String.valueOf(seconds);String min = String.valueOf(minutes);String hur = String.valueOf(hours);String day = String.valueOf(days);if (seconds < 10)sec = "0" + seconds;if (minutes < 10)min = "0" + minutes;if (hours < 10)hur = "0" + hours;if (days < 10)day = "0" + days;output = day + "D " + hur + "H " + min + "M " + sec + "S";} catch (Exception e) {e.printStackTrace();}return output;}
No comments:
Post a Comment