溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Timer and TimerTask計時器

發(fā)布時間:2020-05-18 15:43:26 來源:網(wǎng)絡(luò) 閱讀:619 作者:671076656 欄目:移動開發(fā)

利用Timer和TimerTask做一個計時器

包括開始、停止、暫停、恢復(fù)四個功能

需要注意的問題主要有兩點:

1、Timer和TimerTask在調(diào)用cancel()取消后

不能再執(zhí)行 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)
 * 三個參數(shù)分別為:1、要執(zhí)行的任務(wù) 2、延遲多少時間開始執(zhí)行 3、每隔多少時間執(zhí)行一次
 * */
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>



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI