BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 10-08-2010, 01:51 PM   #1 (permalink)
New Member
 
Join Date: Oct 2010
Model: 8520
PIN: N/A
Carrier: Digitel
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Displaying a timer

Please Login to Remove!

Hey people, i'm making a voice note app and i need to display to the user how long he's been recording and how long he's been playing the voice note, does anyone have any idea on how i can display the timer?
Offline  
Old 10-11-2010, 02:19 PM   #2 (permalink)
New Member
 
Join Date: Oct 2010
Model: 8520
PIN: N/A
Carrier: Digitel
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I managed to display the timer, but now that i did, i'm gettin a RuntimeException when i try to record my voice note on the simulator.

The following code is my StopWatch class which measures the time and displays it on a labelfield on the screen, the error occurs in the run() method when DisplayTime is called

Code:
package Clases;

import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;

public class StopWatch extends Thread {
	   public final static int COUNT_UP   = 0;
	   public final static int COUNT_DOWN = 1;

	   private int type = COUNT_DOWN;
	   private volatile int speedFactor = 1;
	   private long currentTime;
	   private long allowedTime;
	   private long elapsedTime;
	   private long timeChange;
	   private long interval = 50;
	   
	   private LabelField _label;

	   private final Object runLock = new Object();

	   private volatile boolean alive = true;
	   private volatile boolean paused = false;
	   
	   public StopWatch(long ticks, LabelField label){
	      allowedTime = ticks;
	      _label = label;
	   }

	   public StopWatch(int type, long ticks){
	      this.type = type;
	      allowedTime = ticks;
	   }

	   public StopWatch(int type, long ticks, long interval){
	      this.type = type;
	      allowedTime = ticks;
	      this.interval = interval;
	   }

	   public void run(){
	      currentTime = System.currentTimeMillis();
	      while( alive ){
	         if( !paused ){
	            synchronized( runLock ){
	               timeChange = ((System.currentTimeMillis() - currentTime) / speedFactor);
	               elapsedTime = elapsedTime + timeChange;
	               currentTime = System.currentTimeMillis();
	               
	               try {
	            	   if ((elapsedTime/1000)%60 == 0)
		            	   DisplayTime(elapsedTime);
	               }
	               catch (Exception e)
	               {
	            	   Dialog.alert(e.toString());
	               }
	               
	            	  
	              
	              
	            }
	         } else {
	            synchronized( runLock ){
	               currentTime = System.currentTimeMillis();
	            }
	         }
	         try {
	            Thread.sleep(interval);
	         } catch(Exception e) {
	         }
	      }
	   }

	   public void pause(){
	      paused = true;      
	   }

	   public void unpause(){
	      paused = false;
	   }

	   public void stop(){
	      alive = false;
	   }

	   public void reset(){
	      synchronized( runLock ){
	         elapsedTime = 0;
	      }
	   }

	   public long getAllowedTime(){
	      return allowedTime;
	   }

	   public void addTime(long time){
	      synchronized( runLock ){
	         allowedTime = allowedTime + time;
	      }
	   }

	   public void removeTime(long time){
	      synchronized( runLock ){
	         allowedTime = allowedTime - time;
	      }
	   }

	   public void clockFactor(int factor){
	      speedFactor = factor;
	   }

	   public long getTimeMillis(){
	      long remainingTime = 0;
	      if(type == COUNT_DOWN){
	         synchronized( runLock ){
	            remainingTime = allowedTime - elapsedTime;
	            if( remainingTime < 0 ) remainingTime = 0;
	         }
	      } else
	      if(type == COUNT_UP){
	         synchronized( runLock ){
	            remainingTime = elapsedTime;
	            if( remainingTime > allowedTime ) remainingTime = allowedTime;
	         }
	      }

	      return remainingTime;
	   }
	   
	   public void DisplayTime(long timeMillis)
	   {
		    long time = timeMillis / 1000;  
		    
		    String seconds = Integer.toString((int)(time % 60));  
		    String minutes = Integer.toString((int)((time % 3600) / 60));  
		    
		    for (int i = 0; i < 2; i++) {  
		    
		    	if (seconds.length() < 2) {  
		    		seconds = "0" + seconds;  
		    	}  
		    	if (minutes.length() < 2) {  
		    		minutes = "0" + minutes;  
		    	}  
		   }  
		    
		   _label.setText(minutes + ":" + seconds);
	   }

}
Offline  




Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.