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

How-to

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();
nd.registerNotificationObjects();

// Keep this instance around for rendering
// Notification dialogs.
nd.enterEventDispatcher();
}
else
{
// 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.

About Tim W.

Tim works on the Developer Relations team at BlackBerry, focusing on WebWorks, HTML5, and Open Source.

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus