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

Reply
Developer
Posts: 47
Registered: ‎06-05-2009
My Device: Not Specified

Autostartup - SystemListener to ensure safe startup of app question

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/livelink.exe/fetch/2000/348583/800332/832062/How_To_...
 */
class AMCSystemListener implements SystemListener {

    public void DoSafeStartupInitialization() {
        if (ApplicationManager.getApplicationManager().inStartup()) {

// The following line of code generates object undefined exception inside the addSystemListener(this) call. The Application.getApp...() is not // null

           Application.getApplication().addSystemListener(this);
        } 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(this);
        DoStartupWork();
    }

    public void batteryGood() {
        // not used
     }   
   
    public void batteryLow() {
        // not used
     }   

    public void batteryStatusChange(int status) {
        // not used
     }   

    public void powerOff() {
        // not used
    }   
}

Developer
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: Autostartup - SystemListener to ensure safe startup of app question

Where do you create the application instance? (something equivalant to "MyApp appInstance = new MyApp()")

Developer
Posts: 47
Registered: ‎06-05-2009
My Device: Not Specified

Re: Autostartup - SystemListener to ensure safe startup of app question

[ Edited ]

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).

 

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/796557/800738/800901/...

 

The code above is based on this article

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/832062/How_To_...

 

 

Theses two samples should really be merged together or at least the safe initialization added to the alternate entry point sample.

 

Thanks, Andy

Message Edited by aleitzinger on 08-07-2009 08:57 AM
Developer
Posts: 47
Registered: ‎06-05-2009
My Device: Not Specified

Re: Autostartup - SystemListener to ensure safe startup of app question

[ Edited ]

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

Message Edited by aleitzinger on 08-07-2009 12:11 PM
Message Edited by aleitzinger on 08-07-2009 12:12 PM
Highlighted
Developer
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: Autostartup - SystemListener to ensure safe startup of app question

[ Edited ]

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.

 

 

Message Edited by cagdasgerede on 08-07-2009 08:48 AM
Message Edited by cagdasgerede on 08-07-2009 08:52 AM