利用Timer和TimerTask做一个计时器
包括开始、停止、暂停、恢复四个功能
需要注意的问题主要有两点:
1、Timer和TimerTask在调用cancel()取消后
不能再执行 schedule语句,否则提示出错
2、只能在UI主线程中更新控件/组件。
在其他线程中,更新控件/组件,会提示出错
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 MainActivity 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; @Override protected 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); if(!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) * 三个参数分别为:1、要执行的任务 2、延迟多少时间开始执行 3、每隔多少时间执行一次 * */ 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 + ""); } @Override public 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布局
<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>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。