Android中使用定時器的三種方法

本文實例爲你們分享了Android中使用定時器的三種方法,供你們參考,具體內容以下java

由於都比較簡單,因此就直接貼代碼(慮去再次點擊中止的操做),有個全局的Handler負責接收消息更新UIandroid

第一種方法:Thread.sleep();方法app

?ide

1佈局

2post

3this

4spa

5.net

6code

7

8

9

10

11

12

13

14

15

Runnable runnable = new Runnable() {

    @Override

  public void run() {

   while (true) {

   mHandler.sendEmptyMessage(0);

 try {

   Thread.sleep(1000);

  } catch (InterruptedException e) {

    e.printStackTrace();

     }

     }

    }

   };

new Thread(runnable).start();

}

第二種方法:Handler的postDelay()方法

?

1

2

3

4

5

6

7

8

9

10

11

final Runnable runnable = new Runnable() {

      @Override

      public void run() {

        if (isStart2) {

          mHandler.sendEmptyMessage(0);

          mHandler.postDelayed(this, 1000);

        }

      }

    };

mHandler.postDelayed(runnable, 1000);

    }

第三種:Timer和TimerTask

?

1

2

3

4

5

6

7

8

private Timer timer = new Timer();

 private TimerTask timerTask = new TimerTask() {

   @Override

   public void run() {

     mHandler.sendEmptyMessage(0);

   }

 };

timer.schedule(timerTask, 1000, 1000);

總的來講第三種方法最方便,不易出錯,第二種容易忘記添加出發事件.

貼一下完整代碼:

佈局文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

<?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: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="com.brioal.timertest.MainActivity">

 

  <TextView

    android:id="@+id/main_tv"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_centerHorizontal="true"

    android:layout_marginTop="100dp"

    android:text="Hello World!" />

 

  <LinearLayout

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_below="@id/main_tv"

    android:layout_marginTop="100dp"

    android:gravity="center"

    android:orientation="vertical">

 

    <Button

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:layout_gravity="center"

      android:layout_margin="5dp"

      android:onClick="Method1"

      android:text="方法1:Thread"

      android:textAllCaps="false" />

 

    <Button

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:layout_gravity="center"

      android:layout_margin="5dp"

      android:onClick="Method2"

      android:text="方法2:Handler"

      android:textAllCaps="false" />

 

    <Button

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:layout_gravity="center"

      android:layout_margin="5dp"

      android:onClick="Method3"

      android:text="方法3:Task"

      android:textAllCaps="false" />

  </LinearLayout>

</RelativeLayout>

MainActivity

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

package com.brioal.timertest;

 

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.TextView;

 

import java.text.SimpleDateFormat;

import java.util.Timer;

import java.util.TimerTask;

 

public class MainActivity extends AppCompatActivity {

  private TextView mTv;

  private Handler mHandler = new Handler() {

    @Override

    public void handleMessage(Message msg) {

      super.handleMessage(msg);

      //收到消息後顯示當前時間

      long current = System.currentTimeMillis();

      SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

      String time = dateFormat.format(current);

      mTv.setText(time);

    }

  };

  private Timer timer = new Timer();

  private TimerTask timerTask = new TimerTask() {

    @Override

    public void run() {

      mHandler.sendEmptyMessage(0);

    }

  };

  private Thread thread1;

  private boolean isStart1 = false;

  private boolean isStart2 = false;

  private boolean isStart3 = false;

 

 

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    mTv = (TextView) findViewById(R.id.main_tv);

  }

 

  //Thread方法

  public void Method1(View view) {

    Runnable runnable = new Runnable() {

      @Override

      public void run() {

        while (isStart1) {

          mHandler.sendEmptyMessage(0);

          try {

            Thread.sleep(1000);

 

          } catch (InterruptedException e) {

            e.printStackTrace();

          }

        }

 

      }

    };

    if (isStart1) {

      isStart1 = false;

    } else {

      isStart1 = true;

      thread1 = new Thread(runnable);

      thread1.start();

    }

  }

 

  public void Method2(View view) {

 

    final Runnable runnable = new Runnable() {

      @Override

      public void run() {

        if (isStart2) {

          mHandler.sendEmptyMessage(0);

          mHandler.postDelayed(this, 1000);

        }

      }

    };

    if (isStart2) {

      isStart2 = false;

    } else {

      mHandler.postDelayed(runnable, 1000);

      isStart2 = true;

    }

  }

 

  public void Method3(View view) {

    if (isStart3) {

      timer.cancel();

      isStart3 = false;

    } else {

      timer.schedule(timerTask, 1000, 1000);

      isStart3 = true;

    }

  }

}

相關文章
相關標籤/搜索