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.

inside custom component

Java Development

Write safe initialization code

by BlackBerry Development Advisor on ‎02-16-2010 10:14 AM - edited on ‎03-24-2011 11:27 AM by Administrator (9,039 Views)

Summary

 

This article applies to the following:

  • BlackBerry® smartphones based on Java® technology running BlackBerry® Device Software 4.0 and later

Details

 

An application may need to run once automatically during system start-up to perform initialization routines such as registering listeners and retrieving information from persistent storage.

 

Such routines should not be performed until the system has finished core start-up tasks such as security checks, establishing network connectivity, and other tasks.

 

Therefore an application should ensure that system start-up is complete before running its own initialization code, as demonstrated in the following example:

 

 

import net.rim.device.api.system.*;

// application that automatically runs on system startup
class MyApp extends Application implements SystemListener {

public static void main(String [] args) {

MyApp appInstance = new MyApp();

// If system startup is still in progress when this
// application is run.
if (ApplicationManager.getApplicationManager().inStartup()) {
appInstance.addSystemListener(appInstance);
} else {
appInstance.doStartupWorkLater();
}
appInstance.enterEventDispatcher();
}

// constructs
MyApp() {}


private void doStartupWorkLater() {
invokeLater(new Runnable() {
public void run() {
doStartupWork();
}
});
}


private void doStartupWork() {

}


//
// SystemListener
//

/** @see SystemListener#powerUp() */
public void powerUp() {
removeSystemListener(this);
doStartupWork();
}


// TODO: other SystemListener methods

}

 

Contributors
Comments
by Developer on ‎03-21-2011 12:53 PM
class MyApp extends UiApplication implements SystemListener {

I think your code is missing the highlighted addition - please fix.

by Administrator on ‎03-24-2011 11:28 AM

Yes, extends Application should be there.  I'll fix this up.