下面是一个在Android中使用定时器Timer和TimerTask的启动,停止,暂停,继续等操作的demo。
需要注意的问题主要有两点:
1、Timer和TimerTask在调用cancel()取消后不能再执行 schedule语句,否则提示出错,提示如下:
[java] view plaincopyprint?- D/AndroidRuntime(
6672): Shutting down VM - W/dalvikvm(
6672): threadid= 1:thread 0x40018560)exiting with uncaught exception (group= - E/AndroidRuntime(
6672): FATAL EXCEPTION: main - E/AndroidRuntime(
6672): java.lang.IllegalStateException: Timer was canceled - E/AndroidRuntime(
6672): at java.util.Timer.scheduleImpl(Timer.java: - E/AndroidRuntime(
6672): at java.util.Timer.schedule(Timer.java: - E/AndroidRuntime(
6672): at com.snowdream.timerdemo.TimerDemoActivity$ - E/AndroidRuntime(
6672): at android.view.View.performClick(View.java: - E/AndroidRuntime(
6672): at android.view.View$PerformClick.run(View.java: - E/AndroidRuntime(
6672): at android.os.Handler.handleCallback(Handler.java: - E/AndroidRuntime(
6672): at android.os.Handler.dispatchMessage(Handler.java: - E/AndroidRuntime(
6672): at android.os.Looper.loop(Looper.java: - E/AndroidRuntime(
6672): at android.app.ActivityThread.main(ActivityThread.java: - E/AndroidRuntime(
6672): at java.lang.reflect.Method.invokeNative(Native Method) - E/AndroidRuntime(
6672): at java.lang.reflect.Method.invoke(Method.java: - E/AndroidRuntime(
6672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: - E/AndroidRuntime(
6672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: - E/AndroidRuntime(
6672): at dalvik.system.NativeStart.main(Native Method) - W/ActivityManager(
Force finishing activity com.snowdream.timerdemo/.TimerDemoActivity - W/ActivityManager(
Activity forpause timeout HistoryRecord{ 40550560com.snowdream.timerdemo/.TimerDemoActivity} - W/ActivityManager(
Activity fordestroy timeout HistoryRecord{ 40550560com.snowdream.timerdemo/.TimerDemoActivity} - D/dalvikvm(
GC_EXPLICIT 58%freed 13K, free 3127K/7431K, external 0K/0K, paused 70ms - D/dalvikvm(
GC_EXPLICIT 51%freed 59K, free 2935K/5959K, external 245K/512K, paused 84ms - I/ActivityManager(
Start forproc com.android.email service 6691com.android.email/.service.MailService: pid= uid= 10019gids={ 3003,1015}
2、只能在UI主线程中更新控件/组件。在其他线程中,更新控件/组件,会提示出错,提示如下:
(注:这种情况下,可以通过Hander发送消息的方式来更新控件/组件,详情参考例子。)
[java] view plaincopyprint?- E/AndroidRuntime(
6309): android.view.ViewRoot$CalledFromWrongThreadExc eption: Only the original thread that created a view hierarchy can touch its views. - E/AndroidRuntime(
6309): at android.view.ViewRoot.checkThread(ViewRoot.java: - E/AndroidRuntime(
6309): at android.view.ViewRoot.invalidateChild(ViewRoot.java: - E/AndroidRuntime(
6309): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java: - E/AndroidRuntime(
6309): at android.view.ViewGroup.invalidateChild(ViewGroup.java: - E/AndroidRuntime(
6309): at android.view.View.invalidate(View.java: - E/AndroidRuntime(
6309): at android.widget.TextView.checkForRelayout(TextView.java: - E/AndroidRuntime(
6309): at android.widget.TextView.setText(TextView.java: - E/AndroidRuntime(
6309): at android.widget.TextView.setText(TextView.java: - E/AndroidRuntime(
6309): at android.widget.TextView.setText(TextView.java: - E/AndroidRuntime(
6309): at com.snowdream.timerdemo.TimerDemoActivity$ - E/AndroidRuntime(
6309): at java.util.Timer$TimerImpl.run(Timer.java:
Demo源码如下:
TimerDemoActivity.java
- package
com.snowdream.timerdemo; -
- import
java.util.Timer; - import
java.util.TimerTask; -
- import
android.app.Activity; - import
android.os.Bundle; - import
android.os.Handler; - import
android.os.Message; - import
android.util.Log; - import
android.view.View; - import
android.widget.Button; - import
android.widget.TextView; -
- public
class TimerDemoActivity extendsActivity { -
-
static String "TimerDemo";TAG = -
-
TextView null;mTextView = -
Button null;mButton_start = -
Button null;mButton_pause = -
-
Timer null;mTimer = -
TimerTask null;mTimerTask = -
-
Handler null;mHandler = -
-
static int count 0;= -
boolean isPause false;= -
boolean isStop true;= -
-
static int delay 1000;= -
static int period 1000;= -
-
static final int UPDATE_TEXTVIEW 0;= -
-
-
void onCreate(Bundle savedInstanceState) { -
-
setContentView(R.layout.main); -
-
mTextView = (TextView)findViewById(R.id.mytextview); -
mButton_start = (Button)findViewById(R.id.mybutton_start); -
mButton_pause = (Button)findViewById(R.id.mybutton_pause); -
-
-
mButton_start.setOnClickListener( Button.OnClickListener() { -
void onClick(View v) { -
(isStop) { -
Log.i(TAG, -
} { -
Log.i(TAG, -
} -
-
isStop = !isStop; -
-
(!isStop) { -
startTimer(); -
} { -
stopTimer(); -
} -
-
(isStop) { -
mButton_start.setText(R.string.start); -
} { -
mButton_start.setText(R.string.stop); -
} -
} -
}); -
-
mButton_pause.setOnClickListener( Button.OnClickListener() { -
void onClick(View v) { -
(isPause) { -
Log.i(TAG, -
} { -
Log.i(TAG, -
} -
-
isPause = !isPause; -
-
(isPause) { -
mButton_pause.setText(R.string.resume); -
} { -
mButton_pause.setText(R.string.pause); -
} -
} -
}); -
-
mHandler = Handler(){ -
-
-
void handleMessage(Message msg) { -
(msg.what) { -
UPDATE_TEXTVIEW: -
updateTextView(); -
-
-
-
} -
} -
}; -
} -
-
void updateTextView(){ -
mTextView.setText(String.valueOf(count)); -
} -
-
void startTimer(){ -
(mTimer null)== { -
mTimer = Timer(); -
} -
-
(mTimerTask null)== { -
mTimerTask = TimerTask() { -
-
void run() { -
Log.i(TAG, " +String.valueOf(count)); -
sendMessage(UPDATE_TEXTVIEW); -
-
{ -
{ -
Log.i(TAG, -
Thread.sleep( -
} (InterruptedException e) { -
} -
} (isPause); -
-
count ++; -
} -
}; -
} -
-
!= null&& nullmTimerTask != ) -
mTimer.schedule(mTimerTask, delay, period); -
-
} -
-
void stopTimer(){ -
-
(mTimer null)!= { -
mTimer.cancel(); -
mTimer = -
} -
-
(mTimerTask null)!= { -
mTimerTask.cancel(); -
mTimerTask = -
} -
-
count = -
-
} -
-
void sendMessage( intid){ -
(mHandler null)!= { -
Message message = Message.obtain(mHandler, id); -
mHandler.sendMessage(message); -
} -
} - }