Android Open Source - qshuttle_driver Thread Timer






From Project

Back to project page qshuttle_driver.

License

The source code is released under:

MIT License

If you think the Android project qshuttle_driver listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * /*from   w w w  . ja  v a  2s . c  om*/
 */
package com.qshuttle.car;

import java.util.ArrayList;
import java.util.Iterator;

import android.util.Log;

/**
 * @author wangpeifeng
 *
 */
public class ThreadTimer extends Thread{
  
  
  private static final int TIMER_QUICK_START = 1;
  
  private ArrayList<Timer> listTimers;
  
  private int timer_sleep_seconds;
  
  private boolean running;
  
  public ThreadTimer(int seconds){
    
    listTimers = new ArrayList<Timer>();
    
    timer_sleep_seconds = seconds;
    
    running = false;
    
  }

  /* (non-Javadoc)
   * @see java.lang.Thread#run()
   */
  @Override
  public void run() {
    // TODO Auto-generated method stub
    //super.run();
    running = true;
    
    do{
      
//      Log.i("timer", "timer:"+ listTimers.size());

      int i = 0;
      int size = listTimers.size();
      Timer timer;
      
      for( i = size-1; i >= 0; i-- ){
          
        timer = (Timer)(listTimers.get(i));
        
//        Log.i("timer", "timer:"+ Utilities.StampToString(timer.getStart()) + "@" + timer.getTimer());
        
        if(timer.isTimerOut()){
          
          if(timer.isCallBack()){
            
            timer.doCallBack();
          
          }
          
          if(timer.isHandlerMessage()){
            
            timer.sendMessage();
        
          }
          
          if(timer.isRecycle()){
            
            timer.reset();
          
          }
          else{
            
            listTimers.remove(timer);
          
          }
          
        }
        
        
      }
      
      
      
      try{
        
        sleep(timer_sleep_seconds*1000);
        
      }
      catch(Exception e){
        
        e.printStackTrace();
        
      }
      
      
    }while(running);
    
  }
  
  
  public boolean isTimerAdded(Timer timer){
    
    if(listTimers.contains(timer)){
      
      return true;
      
    }
    else{
      
      return false;
    }
    
  }
  
  public void addTimer(Timer timer){
    
    listTimers.add(timer);
    
  }
  
  public void removeTimer(Timer timer){
    
    listTimers.remove(timer);
    
    
  }
  
  public void stopRunning(){
    
    running= false;
    
    this.interrupt();
    
  }
  
    /*
     * 
     * 
     */
    
    public void timerReset(Timer timer){
     
      timer.reset();
      
      if(!this.isTimerAdded(timer)){
        
        this.addTimer(timer);
        
      }
 
    }
    
    
    public void timerReset(Timer timer, long miliseconds){
      
      timer.reset(miliseconds);
      
      if(!this.isTimerAdded(timer)){
        
        this.addTimer(timer);
        
      }

    }
    
    public void timerQuickStart(Timer timer){
      
      timer.reset(TIMER_QUICK_START);
      
      if(!this.isTimerAdded(timer)){
        
        this.addTimer(timer);
        
      }
      
      if(this.getState().equals(Thread.State.NEW)){
      
        this.start();
      
      }
      else if(this.getState().equals(Thread.State.TIMED_WAITING)){
        
        this.interrupt();
      
      }
    
    }
    

}




Java Source Code List

com.qshuttle.car.ActivityAddress.java
com.qshuttle.car.ActivityInfoRequest.java
com.qshuttle.car.ActivityMain.java
com.qshuttle.car.ActivityOfflineMap.java
com.qshuttle.car.DataOperator.java
com.qshuttle.car.DataProvider.java
com.qshuttle.car.DatabaseHelper.java
com.qshuttle.car.ExpandableListAdapter.java
com.qshuttle.car.GPSLocationOverlay.java
com.qshuttle.car.HostSettingActivity.java
com.qshuttle.car.ListAdapterAddress.java
com.qshuttle.car.ListAdapter.java
com.qshuttle.car.LongPressPopupOverlay.java
com.qshuttle.car.MapGestureDetectorOverlay.java
com.qshuttle.car.OverlayMapLocation.java
com.qshuttle.car.PassengerScheduleOverlay.java
com.qshuttle.car.PrefProxy.java
com.qshuttle.car.ThreadHttpPost.java
com.qshuttle.car.ThreadTimer.java
com.qshuttle.car.TimerCallBack.java
com.qshuttle.car.Timer.java
com.qshuttle.car.Utilities.java
com.qshuttle.car.WebApi.java