How to run a timer in the background.

1,161 views
Skip to first unread message

rvac...@gmail.com

unread,
Aug 21, 2012, 8:27:56 AM8/21/12
to codenameone...@googlegroups.com
I need to run a timer which will be set in my application. The duration may be 30 min or even more sometimes therefore the phone should be useful for other tasks while the timer ticks....like play games etc :)
 I would like to set a reminder when 10% of the total time is left. 
Also wondering if i can use multiple timers in the background simultaneously. 

Eric Coolman

unread,
Aug 21, 2012, 8:34:51 AM8/21/12
to codenameone...@googlegroups.com, rvac...@gmail.com
Check out java.util.TimerTask.

Eric

venkatramanan r

unread,
Aug 21, 2012, 9:31:14 AM8/21/12
to codenameone...@googlegroups.com, rvac...@gmail.com
Hi ,

    you can use this code.

Timer t = new Timer();
          TimerTask ta = new TimerTask(){

             public void run() 
             {
                 // code that has to be executed after 3 secs.
             }
          };

t.schedule(ta,3000);


rvac...@gmail.com

unread,
Aug 22, 2012, 7:42:30 AM8/22/12
to codenameone...@googlegroups.com, rvac...@gmail.com
Thank you Eric and Venkatramanan.
Can i also get a countdown clock to peep in for checking remaining time and as said before there may be multiple timers and hence multiple clocks required. 

Shai Almog

unread,
Aug 22, 2012, 9:07:45 AM8/22/12
to codenameone...@googlegroups.com, rvac...@gmail.com
You can have quite a few timers running in parallel. However, don't count on their accuracy and always check the time based on system time.

rvac...@gmail.com

unread,
Apr 9, 2013, 8:38:16 AM4/9/13
to codenameone...@googlegroups.com, rvac...@gmail.com
  1. In your answer above do you mean i should use calendar object and getinstance() method in the timer?
  2. I want the countdown clock set by the timer to run while the application is minimized or even exited. I think the later is not possible (is it?). How to minimize the app and keep the timer/clock running.
  3. Can i show this clock on a glass pane? if yes, any link to a demo/tutorial/code on how glass pane is used?
  4. If there are more than two such clocks running then how can i have them running in a single form/pane/dialogue/(any other most suitable option) one below the other with a title/label on top of each clock for identification.

I am not good in java programming so pl be verbose in your reply. many thanks!!

Shai Almog

unread,
Apr 9, 2013, 2:41:51 PM4/9/13
to codenameone...@googlegroups.com, rvac...@gmail.com
1. Generally I use System.currentTimeMillis() but what works best for you.
2. Which platforms? On RIM, J2ME if it works it works. On iOS you will need to define ios.application_exits to false but there is a time limit after which your app will be closed anyway. On Android this will partially work but if you want to be really thorough you will need native interfaces and a service class.
3. The glasspane javadoc is available, I have a tutorial in my todo.
4. You can show as many clocks as you want.

rvac...@gmail.com

unread,
Apr 10, 2013, 2:16:05 AM4/10/13
to codenameone...@googlegroups.com, rvac...@gmail.com

pl provide the link to the tutorial ...

Shai Almog

unread,
Apr 10, 2013, 12:46:08 PM4/10/13
to codenameone...@googlegroups.com, rvac...@gmail.com
In my "todo" means it doesn't exist. Its a "todo" task.

jam...@fastlaneinnovations.com

unread,
Oct 30, 2014, 12:49:14 AM10/30/14
to codenameone...@googlegroups.com, rvac...@gmail.com
Sorry to revive an old thread, but does this mean that the timers and timer tasks will persist after the app is closed? Can such a timer wake the app and sound an alarm like an alarm clock?

From reading many posts on this forum, I had the impression that a timer wouldn't work like that and I need to look for some native system feature. I found AlarmManager for Android, but even that looks like it will clear your scheduled alarms if someone power cycles the device.

Shai Almog

unread,
Oct 30, 2014, 8:24:25 AM10/30/14
to codenameone...@googlegroups.com, rvac...@gmail.com, jam...@fastlaneinnovations.com
No it won't. For Android you need to create a service, iOS doesn't have something quite like it.
Push notification is the way to do this in the background.
Reply all
Reply to author
Forward
0 new messages