BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 10-08-2010, 01:51 PM   #1
8vius
New Member
 
Join Date: Oct 2010
Model: 8520
PIN: N/A
Carrier: Digitel
Posts: 4
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
8vius
New Member
 
Join Date: Oct 2010
Model: 8520
PIN: N/A
Carrier: Digitel
Posts: 4
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  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


Vintage Mac Tools AW343 Series 1/2 Pneumatic Impact Driver  picture

Vintage Mac Tools AW343 Series 1/2 Pneumatic Impact Driver

$40.00



VINTAGE MSI McHenry Systems MAC IH ELECTRONIC DISTANCE METER SURVEYING picture

VINTAGE MSI McHenry Systems MAC IH ELECTRONIC DISTANCE METER SURVEYING

$322.00



Vintage Mac Tools USA 10” Adjustable Wrench AJC10 Forged USA picture

Vintage Mac Tools USA 10” Adjustable Wrench AJC10 Forged USA

$14.48



Vintage Mac Tools Black Frame Mirror Lens Safety Glasses USA picture

Vintage Mac Tools Black Frame Mirror Lens Safety Glasses USA

$31.88



Vintage Mac Warehouse  3.5” Floppy Disk Solar Powered Calculator Company Swag picture

Vintage Mac Warehouse 3.5” Floppy Disk Solar Powered Calculator Company Swag

$74.00



Vintage UNHOLTZ-DICKIE MAC-6C Equipment - Untested As-is picture

Vintage UNHOLTZ-DICKIE MAC-6C Equipment - Untested As-is

$71.99







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