Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 17
Registered: ‎07-16-2012
My Device: None
My Carrier: None

Java Timers

Hi all,

 

I'm trying to make a timer but if the screen is dormant for 200 seconds it will redirect back to the login screen but the problem is when I go to another screen then more than one thread is running in the background.

 

So when i go to a new screen it must reschedule the timer.

 

How do I go about doing this?

 

public class TimerSettings {
	private Timer timer;
	public TimerSettings(){
		
	}
	public void run(int seconds){
		timer = new Timer();
	    timer.schedule(new TimerTick(), seconds * 1000);
	}
}

 

 

public class TimerTick extends TimerTask {
	private static final long TOKEN_ID = 0x82ef1b6cf42b697aL;
	public void run() {
		UiApplication.getUiApplication().invokeAndWait(new Runnable() {
            public void run() {
            	try{
            		RuntimeStore store = RuntimeStore.getRuntimeStore();
                	store.remove(TOKEN_ID);
                	UiApplication.getUiApplication().pushScreen(new Login());
            	}
            	catch(Exception e){
            	}
            }
        });
	}
}

 

Highlighted
Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Java Timers

Hi @stephenalistoun

 

I suggest to create a central object (accessible from all screens) that would handle the timer and the timer tasks.

 

In this object, keep a reference to the last timer task, and cancel it when the user enters a new screen (a new timer task should be opened).

 

Since you are using the timer for UI actions, you can consider using the invokeLater method with the delay parameter instead of using timers. Again, you will have to keep a reference to the last id returned, so you can cancel the redirect if needed.

Hope that helps,

 

E.