Home > HowTos > Quick WebWorks lesson: how to have your app running in the background

Quick WebWorks lesson: how to have your app running in the background

This is a request I see repeatedly in different forums. Yes, it is possible to have you app running in the background with WebWorks! Having done it recently for GeoBerry, I thought about writing a quick guide for those that want to do it too.

Warning: I’m just giving you the relevant pieces of code, it won’t work if you just copy&paste snippets :)

Prepare the config.xml file

Your config.xml file can take a rim:background element to tell the phone to open a process in the background (documentation). Should look something like this:

<content src="index.html">
  <rim:background src="background.html" runOnStartup="true" />
</content>

index.html is my main page, and background.html is the page that has all the background stuff. The runOnStartup argument is optional. I set it to true because I want the background.html to load when you start up the phone, without needing the user to open it.

Preventing the background process from closing

If you press and hold the BlackBerry menu key, you can see that your app icon will be listed even if your app is not open. This means the background process is running.

Clicking the app icon, will load the main index.html and open the app. If you press the back key to return to the main screen, you will see that your app is now closed and not running in the background anymore.

To prevent this from happening, you need to trap the back key so that instead of closing the app it calls the blackberry.app.requestBackground() function, therefore keeping it running.

Tip: if you are using bbUI.js, edit the js file and search for the popScreen method. Replace blackberry.app.exit() with the requestBackground function call.

Running a function periodically

A common use for this background processes is to periodically run a piece of code. On my GeoBerry app, the user sets the frequency with which the app will request a geolocation and check if there are any reminders in the area.

You can do this easily with the Javascript setTimout() function (documentation).

Here’s a quick example on how to run some code every 5 minutes:

background.html
<html>
 <head>
  <script type="text/javascript" src="background.js" /></script>
 </head>
 <body onload="startBackground();">
 </body>
</html>

 

background.js
var timer;
function doBackground()
{
  // code to repeat goes here...
  ...
  timer = setTimeout("doBackground()", 300000);
} 
function startBackground()
{
  // initialize stuff if needed ...
  ...
  // load the main background function
  doBackground();
}
About these ads
  1. June 29, 2012 at 10:03 am | #1

    #webworks #app #background Hey, nice job ! Thanks !

  2. October 4, 2012 at 1:25 pm | #3

    Hi Thanks for the details, it helped a lot. But my background page is not working. I have tried to start interval using background page, but it is never invoked. Can u help me?

    • October 4, 2012 at 4:20 pm | #4

      Hi!
      Yes I can help, please post in the webworks dev forums, it’s better to discuss it there than here.
      Thanks!

  3. Tony Cheung
    October 22, 2012 at 9:25 am | #5

    Unfortunately the same doesn’t work on BlackBerry Playbook.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 406 other followers