Super Apps Series (Java): Running in the Background Part Two


In this second post in our series on Background Processing (if you missed part one, read it here), we’ll look at using an Alternate Entry Point to separate an application into dedicated foreground and background processes.

An Entry Point is a launcher for an application. Each module with a main() method has an Entry Point, which is the name of the Title of the application and the runtime arguments passed to it, as well as the startup options such as “Auto-run on startup”. An Alternate Entry Point is a way to describe another title and runtime argument for the same application, which can also be configured to run automatically. For example, the main Entry Point could launch your GUI, an alternate could start automatically and register an ApplicationMenuItem, and yet another could take the user directly to a specific screen in the application.

Creating an Alternate Entry Point is done in the BlackBerry® Java® Plugin for Eclipse using the BlackBerry® Application Descriptor. Open this file and select the “AEP” tab in the bottom left. Decide which of the entry points is for the background process, and which will be for the foreground. The names of the entry points should be chosen appropriately, and command line arguments used to tell your main method which process to launch. All Entry Points will launch the main method, which is why application arguments are important.

Here is an example of a main method that handles an argument for an Alternate Entry Point, or runs the regular foreground GUI. This is from the NotificationsDemo which is bundled in the SDK:

public static void main( String[] args )
if( args.length > 0 && args[ 0 ].equals( "autostartup" ) )
NotificationsDemo nd = new NotificationsDemo();

// Keep this instance around for rendering
// Notification dialogs.
// Start a new app instance for GUI operations.
new NotificationsDemo().showGui();

The Alternate Entry Point setup in the NotificationDemo’s BlackBerry Application Descriptor looks as follows:

Note that in this example, the background process is entering the event dispatcher and remaining a running process in the background. In this case, the background process will be pushing dialogs onto the UI stack, so entering the dispatcher and remaining running is necessary. However, it is possible to register a listener object at startup and retain only the object in memory using an API called the RuntimeStore.

The RuntimeStore is a location to share data between processes, by storing Objects indexed by keys. An example in our Knowledge Base shows how to register a listener for incoming mail messages with a listener in the RuntimeStore – one that the GUI portion of the app connects to when it’s run (FolderListener Sample). There are many similar instances where the autostart background process can do initialization and exit without always running. The effect is to create “The Always-on Experience” without necessarily the “Always-on”.

When running with an auto start Alternate Entry Point, keep in mind that each entry point will run as a separate process on the BlackBerry® smartphone. It’s important to make sure that references are not retained between processes when a process exits. This is particularly important to focus on when using listeners; a listener registered from a process that exits will stop the process from being garbage-collected, which will create a memory leak. Thus, it’s critical that every listener is deregistered before the process exits.

Alternate Entry Points create great possibilities for Super Apps to leverage the platform in unique ways. An Alternate Entry Point is the best practice for doing startup tasks in the background. Other possibilities include creating a suite of applications or launchers for different screen; one icon could launch the home screen with a message list, and another could take the user to a dashboard.

Check back next week for part three of this series, where we will look at application state, moving between the background and foreground, and how to use the backlight effectively.

Join the conversation

Show comments Hide comments
+ -
  • Surgana

    great tutorial to read and applicable in my nice blackberry. i have one question, are there some issue about creating java app for blackbarry, there is crashing handset, how to eliminate that case?

  • Swati Srivastava

    Hey this post is really good i have worked on both background and foreground apps and i found that this blog says methodology that i worked on some weeks ago….

    It shows the right way to use both together.

  • Reza Nezami

    Great blog so far. I like to see some more in depth discussion. One scenario I have in mind is an that can launch a background process, after doing its initial gui/forground part, before being closed. The background process will wake up every certain interval (say a few hours or days) and do a certain task and go back to sleep. I like the user be able to control the interval and be able to kill it once the app has served the purpose, say after a week or so. So you see I DON”T want it to be launched at startup or anything like that, rather fully user controlled.

  • Super Apps Series (Java): Running in the Background Part Three | BlackBerry Developer's Blog

    […] well as how to use Alternate Entry Points to run a process in the background or to do setup tasks (part two). Now let’s look at making an application move between foreground and background states, and what […]

blog comments powered by Disqus