Android Studio Phase 44-Countdown

Source: Internet
Author: User

There are a number of ways to Countdown now, here are two methods, code after baptism:

1.CountDownTimer: Call time is very simple: Timer.start (); Timer.cancel ();

private textview vertifyview;      private countdowntimer  Timer = new countdowntimer (10000, 1000)  {              @Override           public  void ontick (long millisuntilfinished)  {               vertifyview.settext (millisuntilfinished / 1000)  +  " Seconds to re-send ");          }              @Override            Public void onfinish ()  {               vertifyview.setenabled (True);               vertifyview.setteXT ("Get Verification Code");           }      } ;

Attached: This method can only support 30 minutes countdown, otherwise there will be a delay. Here is a very accurate method, is more complex ~

2.WeakReference: Countdown to the time difference of one second for each update of the system.

Private h mhandler2 = new h (this);p Rivate static final int msg_ run = 189;private static final int delay_millis = 1000;private  long mcurrenttime;private static class h extends handler {     private weakreference<mainactivity> mactivity;    private  long mnexttime;    public h (mainactivity activity)  {         mActivity = new WeakReference<MainActivity> (activity) ;    }     @Override     public void  Handlemessage (message msg)  {        super.handlemessage (msg) ;         mainactivity activity = mactivity.get ();         if  (activity == null | |  msg.what != msg_run)  {             return;        }         if  (Activity.decrtime (Systemclock.uptimemillis ()  - mnexttime)  {             mnexttime = systemclock.uptimemillis ();             sendemptymessagedelayed (MSG_RUN, DELAY_MILLIS );         }    }}

     support int hour int min int second and direct pass timestamp long 60000 MS, in addition to give you a time of util, hope to help you.

Package com.example.p027_daojishi.util;import java.util.arraylist;import java.util.list;public  class TimeUtil {    /**     *  @param  ms      *  @return  result[day, hour, minute, second]      */    public static long[] compute (LONG&NBSP;MS)  {         long[] result = new long[4];         // day        result[0]  = ms /  (24 * 60 * 60 * 1000);         ms = ms %  (24 * 60 * 60 * 1000);   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;HOUR&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RESULT[1]  = ms /&nBSP; (60 * 60 * 1000);         ms = ms  %  (60 * 60 * 1000);        //  minute        result[2] = ms /  (60 *  ;        ms = ms %  (60 * 1000);         // second         result[3] = ms / 1000;        return result;     }    /**     *  @param       *  @return  void     *  @throws  Exception      *  @throws      *  @Description:  Set the countdown time       */ &nBsp;  public static int[] settime (Int hour)  {         int[] result = new int[2];         //hour  10-bit         result[0] = hour / 10;         //hour  Digit          result[1] = hour - hour / 10 * 10;         return result;    }    /**      * timepicker Set initialization data bufen     *     *  @param  hour     *  @param  num     */     public static list<string> inittptime (final int hour, final  int num)  {        list<string> data = new arraylist <String> ()  {            {                 for  (int i  = hour; i < num; i++)  {                     add (i +  "");                 }             }        };         return data;    }    /**      * timepicker set the start data bufen     * tp_hour.setdata (Settptime (hour, 24)); &NBsp;    * tp_min.setdata (Settptime (min, 60));      *  tp_sec.setdata (Settptime (sec, 60));     * hour=12 num=24      * min=59 num=60     * sec=59 num=60      *     *  @param  hour     *   @param  num     */    public static List< String> settptime (Final int hour, final int num)  {         List<String> data = new ArrayList<String> ()  {            {                 for  (int i = hour;  i < num; i++)  {                     add (i +  "");                 }                 for  (int i = 0; i < hour; i++)  {                      add (i +  "");                 }            }         };        return data;     }    /**     *  get the hour min sec of the countdown layout  bufen 10 : 12 : 59 02 : 01 : 02  turn into  long     *      *  @param  timeStr     *  @return       */    public static long gettimestrsec (String  TIMESTR)  {        if  (timestr == null | |  timestr.length ()  < 5)  {             return -1L;        }         string[] times = timestr.split (":");         if  (times == null | | &NBSP;TIMES.LENGTH&NBSP;!=&NBSP;3)  {             return -1l;        }        if  (times[0] == null | |  times[1] == null | |  times[2] == null)  {             return -1L;        }         long hour = long.parselong (Times[0].trim ());         long minute = long.parselong (Times[1].trim ());         long second = long.parselong (Times[2].trim ());         return hour * 60L * 60L + minute * 60 +  second;    }    /**     * 1 2  3  Transfer  01 02 03     *     *  @param  i     *  @return      */    public static string  time_change_one (long i)  {        string s =  string.format ("%02d",  i);        return s;     }    /**     * 01 02 12 zhuang  1 2 12     *     *  @param  data      *  @return      */    public  static string time_change_two (String data)  {         char[] chars = data.tochararray ();         if  (String.valueof (Chars[0]). Equals ("0"))  {             data =&nbsP String.valueof (chars[1]);        } else {             data = data;         }        return data;    }}

Attached: Timerpicker initialization

Tp_hour.setdata (inittptime (0)), Tp_min.setdata (inittptime (0)), Tp_sec.setdata (inittptime (0, 60));

Summary: In fact, the countdown in order to unify time is the need for accurate calculation, or bad time and the network push delay is likely to affect the results of the settlement, the above methods hope to help you.

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9C/C5/wKiom1l1yTOzNSInAAmsATSGrxI337.png "title=" Qq20170724181618.png "width=" "height=" 333 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:600PX;HEIGHT:333PX; "alt = "Wkiom1l1ytoznsinaamsatsgrxi337.png"/>


This article is from the "Liangxiao Technology Center" blog, please be sure to keep this source http://liangxiao.blog.51cto.com/3626612/1950549

Android Studio Phase 44-Countdown

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.