BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 09-02-2008, 01:38 PM   #1 (permalink)
New Member
 
Join Date: Aug 2008
Model: sim
PIN: N/A
Carrier: sim
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Sample code for background operation?

Please Login to Remove!

New to BB dev and have some basics down, but need to solve this and am not looking to re-invent the wheel. Internet searches have not yielded a great answer here...

Use case.
1. User starts timer that updates UI stopwatch
2. User lets app sit idle or leaves app
a. timer keeps running
3. User returns to app and timer updates to correct time span

Ideas.
A. Figure out background thread operations; solve this way.
B. Fake it by persisting state when app closes or goes idle then regenerate state when app comes back(persist the time closed and diff against time re-opened...then continue if timer is still running);solve this way.

Thanks,
Jimmy

Using JDE 4.2.1
Offline  
Old 09-02-2008, 02:18 PM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2008
Model: 8130
PIN: N/A
Carrier: US Cellular
Posts: 82
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

First of all, you need to persist the time the stopwatch was started, instead of worrying about when your app was or wasn't running. Then the UI side of things is simply showing the difference between now and when the stopwatch started. If you're wanting to fire events (alarm clock, etc) then it's a different story, and you'll want to use some formal scheduling API.

One caveat - you should use some sort of standardized time for the timestamp, so timezone changes won't affect your difference calculation. The devices should switch timezones automatically for people that are traveling.
Offline  
Old 09-02-2008, 08:26 PM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Model: sim
PIN: N/A
Carrier: sim
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks.

I seem to not be very efficient in using search because what I am looking for is a simple example that must exist.

1. User starts timer from menu item
2. UI starts counting
3. User lets app be idle (keeps counting in background)
4. User returns to app
5. UI keeps counting...

Basically this demonstrates updating the UI from a separate thread and dealing with idle apps. I am looking for best practices etc. so I do not re-invent the wheel.

Code sample anyone?
Offline  
Old 09-02-2008, 08:37 PM   #4 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2008
Model: 8130
PIN: N/A
Carrier: US Cellular
Posts: 82
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Using a thread for that is overkill (not to mention that worker threads are limited in how they interact with the UI).

Just do something like this:
Code:
final Timer t=new Timer();
TimerTask tt=new TimerTask() {
    public void run() {
        //Set value of the field that displays the timer value here
    }
};

t.scheduleAtFixedRate(tt, 1000, 1000);
run will be called every second, so you just set the value you want displayed to whatever UI element you've created for that. Or you can call invalidate() within run, and have a paint routine that simply draws the current timer value (if you want to do the drawing yourself).

Last edited by Dan East : 09-02-2008 at 08:38 PM.
Offline  
Old 10-30-2008, 08:26 AM   #5 (permalink)
Thumbs Must Hurt
 
Join Date: Oct 2008
Model: 8800
PIN: N/A
Carrier: AT
Posts: 81
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

In my application i want to set the number to generate the call automatically at a later time.
How we have got to do this?
Offline  
Closed Thread


Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





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