Android-Countdown implementation

Source: Internet
Author: User

Android-Countdown implementation
CountDownTimer class implements the countdown function. Encapsulate background thread creation and Handler queue into a convenient class call. This class is relatively simple and has only four methods: onTick, onFinsh, cancel, and start. The first two are abstract methods, So rewrite them. The following is a small official example: copy the Code new CountdownTimer (30000,100 0) {public void onTick (long millisUntilFinished) {mTextField. setText ("seconds remaining:" + millisUntilFinished/1000);} public void onFinish () {mTextField. setText ("done! ");}}. Start (); copy the Code to copy the Code package com. yydcdut. daojishi; import android. OS. bundle; import android. OS. countDownTimer; import android. app. activity; import android. view. menu; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {private MyCount mc; private TextView TV; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedIn StanceState); setContentView (R. layout. activity_main); TV = (TextView) findViewById (R. id. show); mc = new MyCount (30000,100 0); mc. start () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;}/* defines an internal countdown class */class MyCount extends CountDownTimer {public MyCount (Long millisInFuture, long countDownInterval) {super (millisInFuture, countDownInterval) ;}@ Override public void onFinish () {TV. setText ("finish") ;}@ Override public void onTick (long millisUntilFinished) {TV. setText ("Please wait 30 seconds (" + millisUntilFinished/1000 + ")... "); Toast. makeText (MainActivity. this, millisUntilFinished/1000 + "", Toast. LENGTH_LONG ). show (); // toast has a display time delay.} the copy code mainly involves rewriting onTick and onF. Insh: the code in onFinish () is what you want to do when the timer ends. The code in onTick (Long m) is what you want to do when the countdown starts, the m parameter is the time until completion. In the constructor MyCount (), the former is the number of Countdown times, and the latter is the interval between the second, all are in milliseconds. For example, if the countdown is 30 seconds and the interval between two seconds is 1 second, two parameters can be written as MyCount ). It encapsulates background thread creation and Handler queue into a convenient class call.

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.