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 ...
随机推荐
- Python元组、列表、字典
```python>>> help(tuple) Help on class tuple in module __builtin__: class tuple(object) │ t ...
- SharePoint 2013 弹窗效果之本地HTML打开方式(二)
上一篇我们主要讲述如何通过showModalDialog方法进行弹出窗体,同时弹出信息定义在新的页面(Application Page),使用 SP.UI.$create_DialogOptions( ...
- Redmine配置
官网步骤说明 http://www.redmine.org/projects/redmine/wiki/RedmineInstall 搭环境 1.MySql 2.RailsInstaller:Redm ...
- jQuery.dialog
本篇文章主要是对JQUERY中dialog的用法进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助 今天用到了客户端的对话框,把 jQuery UI 中的对话框学习了一下. 准备 jQ ...
- 又是干货,这个春节感觉吃饱了。各方面---RHCS集群理论
什么是集群? 集群是一组(>2)相互独立的,通过高速网络互联的计算机组成的集合.群集一般可以分为科学集群,负载均衡集群,高可用性集群三大类. 科学集群是并行计算的基础.它对外就好象一 ...
- 硬盘IO,SAS,SATA,和HD TUNE
SAS的接口技术可以向下兼容SATA.具体来说,二者的兼容性主要体现在物理层和协议层的兼容. SAS系统的背板(Backplane)既可以连接具有双端口.高性能的SAS驱动器,也可以连接高容量.低成本 ...
- 使用eclipse搭建嵌入式开发环境
下载jdk http://download.oracle.com/otn-pub/java/jdk/7u4-b20/jdk-7u4-linux-i586.tar.gz 下载eclipse-cpp-ga ...
- 网络流(最大流):POJ 1149 PIGS
PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. 64-bit integer(整数) ...
- QDomDocument Access violation writing location
今天犯了一个非常2的错误! 为了将面板参数保存起来,选择用QDomDocument构造Dom树,然后用doc.toString()方法返回符合xml格式的QString.如: QString CutF ...
- HDOJ(HDU) 2135 Rolling table
Problem Description After the 32nd ACM/ICPC regional contest, Wiskey is beginning to prepare for CET ...