Android Countdown implementation

Source: Internet
Author: User

New product details, to the special sale to increase the countdown function:


Using the Countdowntimer of the Android system, this class


About the usage of this class:


* Schedule a countdown until a time in the

* Regular notifications on intervals along the.

*

* Example of showing a second countdown in a text field:

*

* <pre class= "Prettyprint" >

* New Countdowntimer (30000, 1000) {

*

* public void OnTick (long millisuntilfinished) {

* Mtextfield.settext ("seconds remaining:" + millisuntilfinished/1000);

*     }

*

* public void OnFinish () {

* Mtextfield.settext ("done!");

*     }

*}.start ();


Two methods were added to the Time tool class:

/**

* Get the countdown time interval

*

* @param datestr

* @return

*/

public static long Getcountdowninterval (String datestr) {

Long interval = 0;


if (Stringutils.isblank (DATESTR)) {

return interval;

}

Date countdowndate = new Date ();

try {

Countdowndate = Db_date_format.parse (DATESTR);

} catch (ParseException e) {

Logutils.loge ("Dateutils", "Problem with date format");

}

Date now = new Date ();

Interval = Countdowndate.gettime ()-now.gettime ();

if (Interval < 0)

return 0;

return interval;

}


/**

* @param createtime

* @return Returns the time interval from the current moment, described in Chinese form

*/

public static int[] Getcountdownintervaldetails (long intervalsec) {

int[] countdownintervaldetals = new Int[4];

Intervalsec = intervalsec/1000;//First turn into seconds

Days

int day = (int) (INTERVALSEC/(24 * 3600));

Countdownintervaldetals[0] = day;


Intervalsec = Intervalsec-day * (24 * 3600);


When

int hour = (int) (intervalsec/3600);

Countdownintervaldetals[1] = hour;


intervalsec = Intervalsec-hour * 3600;


Score of

int min = (int) (INTERVALSEC/60);

Countdownintervaldetals[2] = min;


INTERVALSEC = intervalsec-min * 60;


COUNTDOWNINTERVALDETALS[3] = (int) intervalsec;


return countdownintervaldetals;

}


Finally, assemble a control:


/**

* Countdown Control

*/

public class L_countdownblock implements Y_uiblock {


Private Context Mcontext;

Private View container;

Private Countdowntimer CDT;

Private TextView Tv_countdown_day;

Private TextView Tv_countdown_hour;

Private TextView tv_countdown_min;

Private TextView tv_countdown_sec;

protected int[] intervaldetails;


Public L_countdownblock (view view) {

This.container = view;

This.mcontext = Container.getcontext ();

}


/**

* Set End time

*

* @param datestr

*/

private void SetDate (String datestr) {

Long interval = Dateutils.getcountdowninterval (DATESTR);

CDT = new Countdowntimer (interval, 1000) {

@Override

public void OnTick (long millisuntilfinished) {

Intervaldetails = Dateutils

. Getcountdownintervaldetails (millisuntilfinished);

Tv_countdown_day.settext (Intervaldetails[0] + "");

Tv_countdown_hour.settext (Intervaldetails[1] + "");

Tv_countdown_min.settext (intervaldetails[2] + "");

Tv_countdown_sec.settext (Intervaldetails[3] + "");

}


@Override

public void OnFinish () {

TODO adds a countdown to the end of the logic

}


};

Cdt.start ();

}


/**

* Cancel Countdown

*/

private void Cancelcountdown () {

if (null! = CDT) {

Cdt.cancel ();

}

}


@Override

Public <T> void update (T t) {

Tv_countdown_day = (TextView) container

. Findviewbyid (R.id.tv_countdown_day);

Tv_countdown_hour = (TextView) container

. Findviewbyid (R.id.tv_countdown_hour);

Tv_countdown_min = (TextView) container

. Findviewbyid (R.id.tv_countdown_min);

Tv_countdown_sec = (TextView) container

. Findviewbyid (R.ID.TV_COUNTDOWN_SEC);

SetDate ((String) t);

}


@Override

Public View Getcontentview () {

return container;

}

}


Android Countdown implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.