6

我正在尝试创建一个将在系统启动时运行的后台应用程序。当我手动运行它(从功能区)时,屏幕会出现,但是当我将其设置为启动应用程序(描述符中的启动时自动运行选项)后运行该应用程序时,屏幕上不会出现任何内容。我正在尝试以下代码;

public class AppClass extends UiApplication {

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

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

这是屏幕类;

public final class AppScreen extends MainScreen {

    private LabelField  label;

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

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

        add(label);
    }
}

我期待它是一个 UI 应用程序,因此无论是在启动时自动运行还是手动运行,它的屏幕都应该可见。如果我需要做一些事情以使其按预期工作,请指导我,我是 BlackBerry 开发的新手。我正在以下环境中开发;

  • 黑莓 JDE Eclipse 插件 1.5.0
  • 黑莓操作系统 4.5
4

2 回答 2

5

自动启动应用程序在操作系统完成启动之前运行,因此不支持用户界面。我怀疑您的应用程序正在启动,但在某些 UI 调用上失败。编写要自动运行并从主屏幕运行的应用程序的文档化方法是为自动运行提供一个备用入口点,并使用参数告诉程序它已自动运行。然后使用 API 等待操作系统为 UI 应用程序做好准备。

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());
    }
}
于 2012-06-27T12:28:46.230 回答
2

调用getApplication().requestForeground(); 从 AppScreen 类的构造函数中,以便您的屏幕可见。

public final class AppScreen extends MainScreen {

    private LabelField  label;

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

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

        add(label);

        getApplication().requestForeground();
    }
}

一旦应用程序在后台运行,我们必须明确地将其带到前台以显示 UI 元素,这就是我们在这里所做的。

于 2012-07-23T11:57:25.333 回答