views:

678

answers:

1

Hello,

Does TimerTask work even if my exit my application and come to home screen or explore other apps? I have read the forum and find out the thread kind of easy mechanism is to use TimerTask in BB. I want to have thread running always even my app is not launched(after first time), so that when an particular interval arrives, i can show my dialog from application. I tried the below: (i am having this code, not in MainScreenClass instead first push screen class)

 try {
  timer = new Timer();
  // start after 1 second, repeat every 5 second
  // timer.schedule(new ClickTask(), 0, 5000); // seconds*1000
  timer.scheduleAtFixedRate(new ClickTask(), 0, 5000);
 } catch (Exception e) {
  // do nothing
 }

 private class ClickTask extends TimerTask {
  public void run() {
   System.out.println("Test Printing..");
   // Screen screen = new Dialog(Dialog.D_OK, 
   // "Look out!!!", Dialog.OK,
   // Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION),
   // Manager.VERTICAL_SCROLL);
  }
 }

But it does running the timer only when my application is launched. If i quit from my application and come to device home screen, seems to be log is not printing(i.e. timer is not running). Is my code right? Is this the way to run timer in the background as thread? Note: I'm testing on 9530 Simulator for this.

Thanks for your helps.

+2  A: 

TimerTask is for when your application is actually running.

Look at PushRegistry.registerAlarm() for launching your application after an interval.

Kevin Montrose
Thanks. I'll try this and update. One more question, with my above TimerTask code itself, if i use UiApplication.getUiApplication().requestForeground(); in application close, will allow my app to run always and timer also will be always running right?
I'd rather see you use `requestBackground()` so the user gets the illusion of having closed the Application. That said, I think that approach will work; but I haven't used it personally. The `registerAlarm()` method is probably better from a performance perspective.
Kevin Montrose
never tried registerAlarm() myself, think it's better for performance than having your app running in background continuously. The issue is that you need to develop MIDlet to use registerAlarm()...
Max Gontar