您好,登錄后才能下訂單哦!
利用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。