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 ...
随机推荐
- C++实现红黑树,仿STL封装
//RB_Tree.hpp //The code of red black trees //2011/12/31 by Adoo // The foundation :http://www.roadi ...
- IOS--UIImageView的使用方法
IOS--UIImageView的使用方法 //初始化 UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(10 ...
- Why GEMM is at the heart of deep learning
Why GEMM is at the heart of deep learning I spend most of my time worrying about how to make deep le ...
- angular2 学习笔记 (Pipes)
Pipe 就是 ng1 的 filter <pre>{{ jsonValue | json }}</pre> 用法看这里就很清楚了 : https://angular.cn/d ...
- Pots
poj3414:http://poj.org/problem?id=3414 题意:给你两个罐子的体积,然后让你只用这两个罐子量出给定k体积的水.题解:这里可以把两个罐子看成是一个二维的图,然后体积的 ...
- cf C On Number of Decompositions into Multipliers
题意:给你n个数,然后把这个n个数的乘积化成n个数相乘,可以化成多少个. 思路:分解质因数,求出每一个质因子的个数,然后用组合数学中隔板法把这些质因子分成n分,答案就是所有质因子划分成n份的情况的乘积 ...
- 【号外号外:微软收购 .NET 的开源实现 Xamarin 项目的公司】
[首页小编:你好,关于博客园对Xamarin的报道确实一笔而过了,希望能不要把这篇文章移除首页呵呵,祝福帅气,聪明,敏捷,睿智的小编] 一个月后,微软开始免费Xamarin了....还要放开SDK.. ...
- sudo and su
sudo bash : change the current user into root su u1: change the current user into u1 useradd: add ne ...
- COJ 0601&0602 动态规划(二)及加强
未加强传送门0601:http://oj.cnuschool.org.cn/oj/home/addSolution.htm?problemID=571 加强传送门0602:http://oj.cnus ...
- SQL Server判断对象是否存在 (if exists (select * from sysobjects )(转)
1 判断数据库是否存在Sql代码 if exists (select * from sys.databases where name = ’数据库名’) drop database [数据库名] ...