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错误的解决方法【转】的更多相关文章

  1. 错误:java.lang.IllegalStateException: TimerTask is scheduled already

    Process: com.multak.cookaraclient, PID: 27384 java.lang.RuntimeException: Unable to resume activity ...

  2. java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 错误的解决办法

    java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 错误的解决办法 (2011-05-05 16:08:05) 转载▼ ...

  3. 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 ...

  4. java.lang.IllegalStateException: Failed to load ApplicationContext selenium 异常 解决

    WARN <init>, HHH000409: Using org.hibernate.id.UUIDHexGenerator which does not generate IETF R ...

  5. 修改Android 4.2.2的原生Camera引出的java.lang.UnsatisfiedLinkError: Native method not found,及解决方法

    修改Android 4.2.2的原生Camera应用,做一些定制,将Camera的包名从之前的 package com.android.* 修改成了com.zhao3546.*. 调整后,应用可以正常 ...

  6. Android 用webService产生java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive错误的解决(转)

    在做android  Webservice开发的时候一般情况下大家接受webservice服务器返回值的时候都是使用 SoapObject soapObject = (SoapObject) enve ...

  7. java.lang.NoClassDefFoundError: com/gexin/rp/sdk/exceptions/RequestException解决方法

    本文为博主原创,未经允许不得转载: 最近在开发个推的时候遇到的问题,当我在maven仓库中下载个推的jar包时,下载不下来,索性在项目中Configue build Path,将jar下载到本地 手动 ...

  8. 执行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 ...

  9. 关于java.lang.Exception:No tests found matching的一系列解决方法

    问题描述: java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=yahaa], {ExactMatcher ...

随机推荐

  1. 【转】分享II→IV FPGA本人的几个版本电源模块设计的方案

    很多人问我FPGA的电源怎么怎么着,当然也有人瞎忽悠乱设计,当然我的设计也不是很完美...这里把我当年第一次设计FPGA,到现在的电源方案,几个演变.分析的过程,给大家讲讲... (1)FPGA电源方 ...

  2. BZOJ 3240 矩阵游戏

    Description 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的\(n\)行\(m\)列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若用\(F[i][j]\)来 ...

  3. A Statistical View of Deep Learning (IV): Recurrent Nets and Dynamical Systems

    A Statistical View of Deep Learning (IV): Recurrent Nets and Dynamical Systems Recurrent neural netw ...

  4. iOS之H5和Native混合开发

    今天需要用到一个H5和Native 混合开发的项目,简单的写一点入门的东西,很简答: 先介绍一下简单的配置步骤: 1.新建项目:SB拖一个UIWebView 按住Ctrl 拖线delegate 设置为 ...

  5. delphi XML 原来可以玩接口

    以下代码旨在 脱离TXMLDocument 操作 xml unit Unit3; interface uses Windows, Messages, SysUtils, Variants, Class ...

  6. 【HDOJ】2444 The Accomodation of Students

    图论的题目.着色原理+二分图匹配. #include <cstdio> #include <cstring> #define MAXN 205 char map[MAXN][M ...

  7. asp.net 由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值

    看MS给的解决方案:(http://support.microsoft.com/kb/312629/ ) 症状:如果使用 Response.End.Response.Redirect 或 Server ...

  8. Summary Ranges —— LeetCode

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  9. Java---对象与类的封装

    一.类和对象: package cn.hncu.Myclasslearn; /** * * @author hncu_chx * * Mylove amin */ /**类是一种数据类型,声明一个类就 ...

  10. Agri-Net(prim算法,最小生成树问题)

    看图便知道: 来来上代码:         #include <cstdio>#include <cstdlib>#include <cstring>#includ ...