android TimerTask 的简单应用,以及java.lang.IllegalStateException: TimerTask is scheduled already错误的解决方法【转】
Android应用开发中常常会用到定时器,不可避免的需要用到 TimerTask 定时器任务这个类
下面简单的一个示例演示了如何使用TimerTask
这个示例演示了3秒未有触屏事件发生则锁屏(只是设置下文本,意思一下)有触屏事件则解除锁定
public class ColTimerTaskActivity extends Activity {
/** Called when the activity is first created. */
private final String TAG = "ColTimerTaskActivity";
private final int EVENT_LOCK_WINDOW = 0x100;
private TextView textView;
private Handler mHandler;
private Timer mTimer;
private MyTimerTask mTimerTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.textview);
mHandler = new Handler(){
public void handleMessage(Message message){
Log.i(TAG, "message what = " + message.what);
if (message.what == 0x100){
lockWindow();
}
}
};
mTimer = new Timer(true);
resumeWindow();
StartLockWindowTimer();
}
public boolean onTouchEvent(MotionEvent event)
{
// TODO Auto-generated method stub
resumeWindow();
StartLockWindowTimer();
return super.onTouchEvent(event);
}
public void resumeWindow(){
textView.setText("main window");
}
public void lockWindow(){
textView.setText("lock window");
}
public void StartLockWindowTimer(){
if (mTimer != null){
if (mTimerTask != null){
mTimerTask.cancel(); //将原任务从队列中移除
}
mTimerTask = new MyTimerTask(); // 新建一个任务
mTimer.schedule(mTimerTask, );
}
}
class MyTimerTask extends TimerTask{
@Override
public void run() {
// TODO Auto-generated method stub
Log.i(TAG, "run...");
Message msg = mHandler.obtainMessage(EVENT_LOCK_WINDOW);
msg.sendToTarget();
}
}
}
这里需要注意两个问题:
if (mTimerTask != null){
mTimerTask.cancel(); //将原任务从队列中移除
}
每次放定时任务前,确保之前任务已从定时器队列中移除
mTimerTask = new MyTimerTask(); // 新建一个任务
每次放任务都要新建一个对象,否则出现一下错误:
ERROR/AndroidRuntime(): Java.lang.IllegalStateException: TimerTask is scheduled already
所以同一个定时器任务只能被放置一次
android TimerTask 的简单应用,以及java.lang.IllegalStateException: TimerTask is scheduled already错误的解决方法【转】的更多相关文章
- 错误:java.lang.IllegalStateException: TimerTask is scheduled already
Process: com.multak.cookaraclient, PID: 27384 java.lang.RuntimeException: Unable to resume activity ...
- java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 错误的解决办法
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 错误的解决办法 (2011-05-05 16:08:05) 转载▼ ...
- Spring Scheduled定时任务报错 java.lang.IllegalStateException: Encountered invalid @Scheduled method 'xxx': For input string: "2S"
报错信息如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ding ...
- java.lang.IllegalStateException: Failed to load ApplicationContext selenium 异常 解决
WARN <init>, HHH000409: Using org.hibernate.id.UUIDHexGenerator which does not generate IETF R ...
- 修改Android 4.2.2的原生Camera引出的java.lang.UnsatisfiedLinkError: Native method not found,及解决方法
修改Android 4.2.2的原生Camera应用,做一些定制,将Camera的包名从之前的 package com.android.* 修改成了com.zhao3546.*. 调整后,应用可以正常 ...
- Android 用webService产生java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive错误的解决(转)
在做android Webservice开发的时候一般情况下大家接受webservice服务器返回值的时候都是使用 SoapObject soapObject = (SoapObject) enve ...
- java.lang.NoClassDefFoundError: com/gexin/rp/sdk/exceptions/RequestException解决方法
本文为博主原创,未经允许不得转载: 最近在开发个推的时候遇到的问题,当我在maven仓库中下载个推的jar包时,下载不下来,索性在项目中Configue build Path,将jar下载到本地 手动 ...
- 执行Hive时出现org.apache.hadoop.util.RunJar.main(RunJar.java:136) Caused by: java.lang.NumberFormatException: For input string: "1s"错误的解决办法(图文详解)
不多说,直接上干货 问题详情 [kfk@bigdata-pro01 apache-hive--bin]$ bin/hive Logging initialized -bin/conf/hive-log ...
- 关于java.lang.Exception:No tests found matching的一系列解决方法
问题描述: java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=yahaa], {ExactMatcher ...
随机推荐
- PHP学习之中数组--创建数组【1】
在PHP中数组的定义有三种写法,分别是: <?php //第一种方式是用关键字 array来创建的 $username = array("demo1","demo2 ...
- objective-c基础教程
command+shift+c foundation框架包含的头文件 /system/library/frameworkks/foundation.framework/headers/
- 转:理解 PHP 中的 Streams
本文转自:开源中国社区 [http://www.oschina.net]本文标题:理解 PHP 中的 Streams 本文地址:http://www.oschina.net/translate/und ...
- matlab取整
matlab取整 Matlab取整函数有: fix, floor, ceil, round.取整函数在编程时有很大用处.一.取整函数1.向零取整(截尾取整)fix-向零取整(Round towards ...
- [原创]leet code - path sum
; ; ; } } ; }};
- LPSTR、LPCSTR、LPTSTR、LPCTSTR、LPWSTR及LPCWSTR的意义及区别
引用 LPSTR.LPCSTR.LPTSTR.LPCTSTR.LPWSTR及LPCWSTR的意义及区别 1.ANSI(即MBCS):为多字节字符集,它是不定长表示世界文字的编码方式.ANSI表示英文字 ...
- Python操作Excel_输出所有内容(包含中文)
python 2.7.5代码: # coding=utf-8 import sys import xlrd data=xlrd.open_workbook('D:\\menu.xls') table ...
- Search Insert Position——LeetCode
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Java系统变量设置方式
近期碰到一个编码的问题,发现整个平台都是用的GB2312,因此导致webservice调用时有些字不能正常接受. 反编译中间件的源码如下: public static final String nod ...
- zoj 1372
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1372 #include<iostream> #include& ...