6

I am trying to create a background app which will run at system startup. When I run it manually (from the ribbon), the screen appears but when I run the app after making it a startup app (Auto-run on startup option in descriptor), nothing appears on screen. I am trying the following code;

public class AppClass extends UiApplication {

    public static void main(String[] args) {
        AppClass theApp = new AppClass();
        theApp.enterEventDispatcher();
    }

    public AppClass() {
        pushScreen(new AppScreen());
    }
}

And this is the screen class;

public final class AppScreen extends MainScreen {

    private LabelField  label;

    public AppScreen() {
        setTitle("AppTitle");

        label = new LabelField();
        label.setText("Ready.");

        add(label);
    }
}

I am expecting that its a UI app so its screen should be visible no matter if is auto-run at startup or run manually. If I need to do something to make it work as expected, please guide me about it, I am new to BlackBerry development. I am developing in the following environment;

  • BlackBerry JDE Eclipse Plugin 1.5.0
  • BlackBerry OS 4.5
Mudassir
  • 13,962
  • 8
  • 60
  • 85

2 Answers2

5

Auto start applications are run before the OS has completed booting so there isn't any support for the user interface. I suspect your application is being launched but failing on some UI call. The documented way to write an application that is to auto run and run from the home screen is to provide an alternated entry point for the auto run with arguments that tell the program it has been auto run. Then use the API to wait until the OS is ready for UI applications.

public class AppClass extends UiApplication {
    public static void main(String[] args) {

        if (args.length > 0 && args[0].equals("auto-run")) {
            // auto start, wait for OS
            while (ApplicationManager.getApplicationManager().inStartup()) {
               Thread.sleep(10000);
            }

            /*
            ** Do auto-run UI stuff here
            */
        } else {
            AppClass theApp = new AppClass();
            theApp.enterEventDispatcher();
        }
    }

    public AppClass() {
        pushScreen(new AppScreen());
    }
}
Richard
  • 8,950
  • 2
  • 16
  • 24
2

Call getApplication().requestForeground(); from the constructor of your AppScreen class so that your screen will be visible.

public final class AppScreen extends MainScreen {

    private LabelField  label;

    public AppScreen() {
        setTitle("AppTitle");

        label = new LabelField();
        label.setText("Ready.");

        add(label);

        getApplication().requestForeground();
    }
}

Once the app is running in background, we have to bring it to foreground explicitly to show UI element and that is what we are doing here.

Khan
  • 599
  • 3
  • 13