Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Create a Background Application

by BlackBerry Development Advisor ‎02-16-2010 03:23 PM - edited ‎09-16-2010 02:52 PM

Summary

 

This article applies to the BlackBerry® wireless devices based on Java™.

 


Description

 

Some applications may not require a graphical user interface (GUI), but simply perform background processing for the user. The sample application below can initiate when a user selects it from the ribbon; however, no change appears on the screen and the user can select another icon from the ribbon and continue to use the BlackBerry device as they normally would. Selecting the same icon a second time cannot spawn a second instance of this sample, as the device is aware that the application has already been launched and will not create a second instance of it. The following sample will run indefinitely until the device is shut down:

 

 

/** 
* GUILessApplication
*
* A sample application that appears on the ribbon
* but does not have any user interface.
*/

package com.samples.guiLessApplication;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

public final class GUILessApp extends Application
{
private BackGroundApp backGroundApp;
public static void main(String[] args)
{
GUILessApp theApp = new GUILessApp();
theApp.enterEventDispatcher();
}

public GUILessApp()
{
//Creates and starts a new BackGroundApp thread.

backGroundApp = new BackGroundApp();
backGroundApp.start();
}

//The thread that will run in the background.
private class BackGroundApp extends Thread
{
boolean stopThread = false;
public synchronized void stop()
{
stopThread = true;
}

public void run()
{
while (!stopThread)
{
//You would perform your processing here.
//This sample just prints out a line
//to the BlackBerry JDE Output Window
System.out.println("Application is running");

//Sleep for 5 seconds to prevent the
//application from running out of control
try
{
sleep(5000);
}
catch (Exception e)
{
//Exception handling would go here.
}
}
}
}
//Stop the thread on exit.
protected void onExit()
{
backGroundApp.stop();
}
}

 

 

Contributors