Code example for Timer

Methods: schedule

0
	public ActivityUtils(Instrumentation inst, Activity activity, Sleeper sleeper) {
		this.inst = inst;
		this.activity = activity;
		this.sleeper = sleeper;
		createStackAndPushStartActivity(); 
		activitySyncTimer = new Timer();
		activitiesStoredInActivityStack = new Stack<String>();
		setupActivityMonitor(); 
		setupActivityStackListener(); 
	} 
 
 
 
	/** 
	 * Creates a new activity stack and pushes the start activity  
	 */ 
 
	private void createStackAndPushStartActivity(){ 
		activityStack = new Stack<WeakReference<Activity>>();
		if (activity != null){
			WeakReference<Activity> weakReference = new WeakReference<Activity>(activity);
			activity = null;
			activityStack.push(weakReference);
		} 
	} 
 
	/** 
	 * Returns a {@code List} of all the opened/active activities. 
	 *  
	 * @return a {@code List} of all the opened/active activities 
	 */ 
 
	public ArrayList<Activity> getAllOpenedActivities()
	{ 
		ArrayList<Activity> activities = new ArrayList<Activity>();
		Iterator<WeakReference<Activity>> activityStackIterator = activityStack.iterator();
 
		while(activityStackIterator.hasNext()){
			Activity  activity = activityStackIterator.next().get();
			if(activity!=null)
				activities.add(activity);
		} 
		return activities;
	} 
 
	/** 
	 * This is were the activityMonitor is set up. The monitor will keep check 
	 * for the currently active activity. 
	 */ 
 
	private void setupActivityMonitor() { 
 
		try { 
			IntentFilter filter = null;
			activityMonitor = inst.addMonitor(filter, null, false);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	} 
 
	/** 
	 * This is were the activityStack listener is set up. The listener will keep track of the 
	 * opened activities and their positions. 
	 */ 
 
	private void setupActivityStackListener() { 
		TimerTask activitySyncTimerTask = new TimerTask() {
			@Override 
			public void run() { 
				if (activityMonitor != null){
					Activity activity = activityMonitor.getLastActivity();
					if (activity != null){
 
						if(!activitiesStoredInActivityStack.isEmpty() && activitiesStoredInActivityStack.peek().equals(activity.toString())){
							return; 
						}						 
						if (activitiesStoredInActivityStack.remove(activity.toString())){
							removeActivityFromStack(activity);
						} 
						if (!activity.isFinishing()){
							addActivityToStack(activity);
						} 
					} 
				} 
			} 
		}; 
		activitySyncTimer.schedule(activitySyncTimerTask, 0, ACTIVITYSYNCTIME);
	} 
 
	/** 
	 * Removes a given activity from the activity stack 
	 *