Use timer and timertask to make a timer
Including start, stop, pause, and resume
There are two main issues to note:
1. After timer and timertask cancel (),
You cannot execute the schedule statement. Otherwise, an error is prompted.
2. You can only update controls/components in the main UI thread.
Updating controls/components in other threads will prompt an error
Package COM. example. testtimer2; import Java. util. timer; import Java. util. timertask; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; import android. annotation. suppresslint; import android. app. activity; @ suppresslint ("handlerleak") public class mainactiv Ity extends activity implements onclicklistener {private button btnstart; private button btnpause; private Boolean isstop = true; private Boolean ispause = false; private int COUNT = 0; private int delay_time = 1000; private int update_ui = 0x11; private timer mtimer; private timertask mtimertask; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); btnstart = (button) findviewbyid (R. id. button1); btnpause = (button) findviewbyid (R. id. button2); btnstart. setonclicklistener (this); btnpause. setonclicklistener (this);} private void starttimer () {If (mtimer = NULL) {mtimer = new timer ();} If (mtimertask = NULL) {mtimertask = new timertask () {public void run () {do {try {thread. sleep (delay_time); mhandler. sendemptymessage (update_ui); I F (! Ispause) {count ++;} If (isstop) {COUNT = 0 ;}} catch (exception e) {e. printstacktrace () ;}} while (! Isstop) ;}};}if (mtimer! = NULL & mtimertask! = NULL) {/*** timer. schedule (timertask task, long delay, long period) * three parameters are: 1. The task to be executed. 2. The delay time to start execution. 3. The interval to execute the task. **/mtimer. schedule (mtimertask, delay_time) ;}} private void stoptimer () {If (mtimer! = NULL) {mtimer. Cancel (); mtimer = NULL;} If (mtimertask! = NULL) {mtimertask. cancel (); mtimertask = NULL;} COUNT = 0;} private handler mhandler = new handler () {public void handlemessage (Message MSG) {Switch (MSG. what) {Case 0x11: initui (); break ;}}; private void initui () {(textview) findviewbyid (R. id. textview1 )). settext (count + "") ;}@ overridepublic void onclick (view arg0) {If (arg0.equals (btnstart) {If (isstop) {starttimer ();} else {stoptimer ();} isstop =! Isstop; If (isstop) {btnstart. settext (R. string. start_time);} else {btnstart. settext (R. string. stop_time) ;}} if (arg0.equals (btnpause) {If (ispause) {} else {} ispause =! Ispause; If (ispause) {btnpause. settext (R. String. resume_time);} else {btnpause. settext (R. String. pause_time );}}}}
// Layout Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="23dp" android:text="@string/show_time" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView1" android:layout_marginTop="38dp" android:text="@string/start_time" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_alignParentRight="true" android:layout_below="@+id/button1" android:layout_marginTop="16dp" android:text="@string/pause_time" /></RelativeLayout>
// String. xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">TestTimer2</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="start_time">start</string> <string name="stop_time">stop</string> <string name="resume_time">resume</string> <string name="pause_time">pause</string> <string name="show_time">time</string></resources>
This article from the "climb the mountains have seen the sea" blog, please be sure to keep this source http://670176656.blog.51cto.com/4500575/1548690
Timer and timertask Timer