黑莓:提前显示提醒/状态/对话框并退出

时间:2011-08-08 17:24:17

标签: blackberry screen alert exit status

假设我有一个典型的Blackberry应用程序:

public class MyApp extends UiApplication {
    public static void main(String[] args) {
        MyApp app = new MyApp();
        app.enterEventDispatcher();
    }

    public MyApp() {
        pushScreen(new MyScreen());
    }    
}

并且在开始时我注意到,缺少强制条件(显示尺寸错误;缺少SD卡;某些IT政策等等)

有没有办法向用户显示简短快速的消息(以警报/状态/对话框/其他形式)并立即退出 - 没有/在实例化复杂屏幕/注册加速监听器/安装复杂的CleanupRunnable之前?

我尝试了Status.show(),Dialog.alert() - 它们不起作用(RuntimeException“由非事件线程调用的pushModalScreen”):

public class MyScreen extends MainScreen {
    public MyScreen() {
        if (Display.getWidth() < 400) {
            Status.show("Goodbye");
            return;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

使用invokeLater而不是直接调用。样品如下:

Application.getApplication().invokeLater(new Runnable() {
    public void run() {
        Dialog.inform("Your message here...");
    }
});

您可以使用Status.show()

代替Dialog.inform

答案 1 :(得分:2)

实际上以下情况比Rafael建议的更好 - 因为它下面没有丑陋的白色屏幕。这是我的完整示例MyApp.java:

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.image.*;

public class MyApp extends UiApplication {
    public static void main(String[] args) {
        MyApp app = new MyApp();
        app.enterEventDispatcher();
    }

    public MyApp() {
        pushScreen(new MyScreen());
    }    
}

class MyScreen extends MainScreen implements DialogClosedListener {
    Dialog myDialog = new Dialog(Dialog.OK, "Goodbye!", 0, Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), Dialog.GLOBAL_STATUS);

    public MyScreen() {
        // XXX just some condition, like wrong dimensions or IT policy
        if (Display.getWidth() > 40) {
            myDialog.setDialogClosedListener(this);
            Application.getApplication().requestBackground();
            Application.getApplication().invokeLater(new Runnable() {
                public void run() {
                    myDialog.show();
                }
            });
            return;
        }

        // XXX heavy stuff to be skipped
    }

    public void dialogClosed(Dialog dialog, int choice) {
        if (dialog == myDialog) {
            System.out.println("XXX exiting XXX");
            System.exit(1);
        }
    }
}