2012. 3. 21. 02:52
1 package exm.timer;
2
3 import java.util.*;
4
5 import android.app.Activity;
6 import android.os.Bundle;
7 import android.util.*;
8 import android.view.*;
9 import android.view.View. Listener;
10 import android.widget.*;
11
12 public class TimerExample extends Activity {
13
14 private TimerTask mTimer;
15 private Button mBtnStart, mBtnStop;
16 private Timer timer = new Timer();
17
18 /-* Called when the activity is first created. *-
19 @Override
20 public void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.main);
23
24 initSetting();
25 btnEvent();
26 }
27
28 private void initSetting() {
29 mBtnStart = (Button) findViewById(R.id.btnstart);
30 mBtnStop = (Button) findViewById(R.id.btnstop);
31 }
32
33 private void btnEvent() {
34 mBtnStart.set Listener(new Listener() {
35
36 @Override
37 public void (View v) {
38 TimerMethod();
39 }
40 });
41
42 mBtnStop.set Listener(new Listener() {
43
44 @Override
45 public void (View v) {
46 stopTimer();
47 }
48 });
49 }
50
51 private void TimerMethod() {
52 mTimer = new TimerTask() {
53
54 @Override
55 public void run() {
56 Log.d("TIMER", "" + System.currentTimeMillis());
57 }
58 };
59
60 timer.schedule(mTimer, 2000, 3000);
61 }
62
63 private void stopTimer() {
64 // timer.cancel();
65 mTimer.cancel();
66 }
67
68 }
Posted by k1rha