08-06-2009 04:54 PM
Hi, I used the KB article regarding implementing a systemlistener to ensure that an application that is set for autostart initializes after the system is loaded. I'm developing with JDE 4.5.
I modified the example class so that my UI Application which contains the Main() instantiates an instance of my AMCSsystemListener if the app is launched via alternate entry point.It blows up saying there is no Application instance trying to addSystemListener(...)
package com.Alert.AlertMobileClient;
import net.rim.device.api.system.*;
// application that automatically runs on system startup
/**
* class AMCSystemListener
*
* Ensures that the BB is properly initialized before the application is started in the
* background on startup of the BB
* See: http://www.blackberry.com/knowledgecenterpublic/li
*/
class AMCSystemListener implements SystemListener {
public void DoSafeStartupInitialization() {
if (ApplicationManager.getApplicationManager().inStar
// The following line of code generates object undefined exception inside the addSystemListener(this) call. The Application.getApp...() is not // null
Application.getApplication().addSystemListener(thi
} else {
DoStartupWorkLater();
}
}
// constructs
AMCSystemListener() {}
private void DoStartupWorkLater() {
Application.getApplication().invokeLater(new Runnable() {
public void run() {
DoStartupWork();
}
});
}
private void DoStartupWork() {
/**
* Retrieving the GlobalRef() forces the singleton instance of the AMCApplication class to be constructed
* the first time it is invoked
*/
AMCApplication.getGlobalRef();
}
//
// SystemListener
//
/** @see SystemListener#powerUp() */
public void powerUp() {
Application.getApplication().removeSystemListener(
DoStartupWork();
}
public void batteryGood() {
// not used
}
public void batteryLow() {
// not used
}
public void batteryStatusChange(int status) {
// not used
}
public void powerOff() {
// not used
}
}
08-06-2009 09:11 PM
Where do you create the application instance? (something equivalant to "MyApp appInstance = new MyApp()")
08-07-2009 08:53 AM - edited 08-07-2009 08:57 AM
I have 3 classes, MyUIApp, My App and this utility class, the systemlistener.
The static Main() method is in MyUIApp. However, since I based my code on the KB sample app for creating an GUI app (link below) that also has a folder listener that runs in the background, the MYUIApp does not get created if the Main() is entered via the alternate entry mode (background).
The code above is based on this article
Theses two samples should really be merged together or at least the safe initialization added to the alternate entry point sample.
Thanks, Andy
08-07-2009 09:42 AM - edited 08-07-2009 12:12 PM
So, It looks like the KB SAMPLE APP for safe initialization IS WRONG!
The class implements SystemListener but it also needs to extend Application. I didn't run the sample code first so I didn't notice it.
Once I changed my class to implement UIApplication I got it working.
It's hard enough figuring out how to get stuff to work without having to solve errors in KB samples! 8(
Regards
08-07-2009 11:42 AM - edited 08-07-2009 11:52 AM
You are right. There is a typo in the example (by the way, I think you meant "extends"). Also I think it can extend UiApplication or Application.
I agree that those articles are related, they should at least include a link to each other.