zoukankan      html  css  js  c++  java
  • 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, 3000);
         }
        }
        
        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(11761): Java.lang.IllegalStateException: TimerTask is scheduled already


          所以同一个定时器任务只能被放置一次

     
     
  • 相关阅读:
    php 7.1 openssl_decrypt() 代替 mcrypt_module_open() 方法
    关于Http_build_query的用法
    git fetch 和git pull 的差别
    PhpStorm 头部注释、类注释和函数注释的设置
    输出信息log4j.properties的作用与使用方法
    字段设置ALV中下拉列表列的实现
    遍历中序C语言实现二叉树的递归遍历与非递归遍历
    搜索中文Solr Analysis And Solr Query Solr分析以及查询
    记忆指向指针常量 常量指针 常量指针常量
    匹配位置KMP算法深入浅出
  • 原文地址:https://www.cnblogs.com/Sharley/p/5953040.html
Copyright © 2011-2022 走看看