The front for you to talk about the two clockwise method of the timer, in the recording of video and other operations quite use, today brings you the countdown to achieve two ways.
Although the recent writing is relatively simple and basic, but the simple does not represent the familiar, the basis does not represent will, Daniel Bypass, ha, Niu calf can also bypass, this is addressed to beginners.
Get an effect chart first.
Code implementation is also super simple, here is the first way to implement, but also more suitable for everyone, is through direct inheritance countdowntimer to achieve.
For Countdowntimer This class is simple, it must be inherited by rewriting the construction method and implementing its virtual methods.
The two parameters of the construction method are (countdown start time, Interval time)
The other two methods are Ontick (now the remaining time), and the time you want to do after the end of the clock can be done in onfinish ().
The value of the note is that all the time is done in milliseconds, so you need to remember to divide 1000 to get the quotient when you use it.
However, since I am using a private internal class to refer to an external class, and to prevent memory leaks, we should be careful to empty the activity when it is destroyed, and we should also avoid creating objects repeatedly.
Another way to do this is to use our usual handler + thread approach. However, the implementation of the same time to be very careful memory leaks, because if the user in destroying the activity should be careful to let its timer thread no longer loop, this can be set by a tag tag to judge it.
So in the destruction of the time to put this tag tag to false, end the execution of the thread!
Here is the implementation code:
Package Com.example.nanchen.timerdemo;
Import Android.os.Bundle;
Import Android.os.CountDownTimer;
Import Android.os.Handler;
Import Android.os.Message;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
public class Mainactivity extends Appcompatactivity {private Button mbtngetcode;
Private Timecount Mtimecount;
Private Button MBtnGetCode2;
Private Boolean timeflag = true;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mbtngetcode = (Button) Findviewbyid (R.id.main_btn_get_code);
Mbtngetcode.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {mtimecount = null;
Mtimecount = new Timecount (60 * 1000, 1000);
Mtimecount.start ();
}
});
MBtnGetCode2 = (Button) Findviewbyid (r.id.main_btn_get_code_2); Mbtngetcode2.setonclicklistener (New OnclickliStener () {@Override public void OnClick (View v) {mbtngetcode2.setclickable (false);
Mbtngetcode2.setbackgroundcolor (Getresources (). GetColor (r.color.btn_unable));
Timeflag = true;
New Thread () {@Override public void run () {super.run ();
for (int i = >= I-0 && timeflag; i--) {try {sleep (1000);
Message msg = Message.obtain ();
Msg.what = i;
Mhandler.sendmessage (msg);
catch (Interruptedexception e) {e.printstacktrace ();
}}}.start ();
}
}); Private Handler Mhandler = new Handler () {@Override public void Handlemessage (msg) {Super.handlemessage (
msg);
if (Msg.what > 0) {mbtngetcode2.settext ("+ Msg.what +") seconds after Retry ");
else {mbtngetcode2.settext ("Get Authentication Code");
Mbtngetcode2.setclickable (TRUE);
Mbtngetcode2.setbackgroundcolor (Getresources (). GetColor (r.color.coloraccent));
}
}
}; /** * When the activity is destroyed, note that all references are empty to prevent memory leaks//@Override protected void OnDestroy () {SupeR.ondestroy ();
Mtimecount = null;
Timeflag = false; /** * Implementation Countdown class * * Private class Timecount extends Countdowntimer {/** * @param millisinfuture the number of mil Lis in the future from the call * to {@link #start ()} until the countdown are done and {@link #onFinish ()} * is C
Alled.
* @param countdowninterval the interval along the way to receive * {@link #onTick (Long)} callbacks.
* * Public Timecount (long millisinfuture, long Countdowninterval) {super (millisinfuture, countdowninterval); The/** * Timing process Display button is not available set to Gray * * @param millisuntilfinished/@Override public void Ontick (Long Millisuntilfini
Shed) {mbtngetcode.setclickable (false);
Mbtngetcode.setbackgroundcolor (Getresources (). GetColor (r.color.btn_unable));
Mbtngetcode.settext ("(" + millisuntilfinished/1000 + ") seconds after Retry");
/** * Timer End call/@Override public void OnFinish () {mbtngetcode.setclickable (true);
Mbtngetcode.settext ("Get Authentication Code Mode 1"); Mbtngetcode.setbackgroundcolor (Getresources (). GetColor (R.color.colorprimarydark));
}
}
}
Take a quick look at the XML file
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:id=" @+id/activity_main "android:layout_width=" Match_parent "android:layout_height=" match_parent "tools:context=" com.example.nanchen.timerdemo.MainActivity " > <button android:layout_margintop= "10dp" android:layout_width= "match_parent" android:layout_height= "Wrap_ Content "android:id=" @+id/main_btn_get_code "android:text=" Get Authentication code Mode 1 "android:background=" @color/colorprimarydark " /> <textview android:layout_width= "match_parent" android:layout_height= "1DP" android:id= "@+id/main_line1" and roid:background= "@color/btn_unable" android:layout_below= "@+id/main_btn_get_code" android:layout_margintop= "10DP "/> <button android:layout_width=" match_parent "android:layout_height=" wrap_content "android:layout_below=" @ +id/main_line1 "android:layout_margintop=" 10DP "android:text=" Get Authentication code mode 2 "
Android:id= "@+id/main_btn_get_code_2" android:background= "@color/coloraccent"/> </RelativeLayout>
Write in the end: Although the code and implementation are very simple, you may be effortless, but if reproduced, or leave a link to this article ~thank you!
GitHub Link: Https://github.com/nanchen2251/TimerDemo
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.