麻烦将黑莓应用程序带到前台

时间:2010-05-07 19:44:44

标签: blackberry

我有一个在后台聆听的应用,当用户点击“发送”时,它会显示一个对话框。但是我需要将我的应用程序带到前台,以便用户在发出消息之前回答一些问题。但我无法做到这一点,这是我的SendListener中的代码:

SendListener sl = new SendListener(){

                public boolean sendMessage(Message msg){

                    Dialog myDialog = new Dialog(Dialog.D_OK,
                        "message from within SendListener",
                        Dialog.OK,Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION),
                        Dialog.GLOBAL_STATUS)
                    {
                         //Override inHolster to prevent the Dialog from being dismissed
                         //when a user holsters their BlackBerry. This can
                         //cause a deadlock situation as the Messages
                         //application tries to save a draft of the message
                         //while the SendListener is waiting for the user to
                         //dismiss the Dialog.

                         public void inHolster()
                         {

                         }
                    };
                    //Obtain the application triggering the SendListener.
                    Application currentApp = Application.getApplication();

                    //Detect if the application is a UiApplication (has a GUI).
                    if( currentApp instanceof UiApplication )
                    {
                        //The sendMessage method is being triggered from
                        //within a UiApplication.
                        //Display the dialog using is show method.
                        myDialog.show();
                       App.requestForeground();
                    }
                    else
                    {
                         //The sendMessage method is being triggered from
                         // within an application (background application).
                         Ui.getUiEngine().pushGlobalScreen( myDialog, 1,
                             UiEngine.GLOBAL_MODAL );
                    }
                    return true;
                }
            };
            store.addSendListener(sl);

App是我在上面创建的对象:

Application App = Application.getApplication();

我还尝试使用其processID调用App前景,但到目前为止还没有运气。

3 个答案:

答案 0 :(得分:1)

我已经设法实现了与你所描述的类似的东西,但不同的是,我的对话框是异步显示的,实际上可能更容易......所以在你的情况下......

第一个我可以建议你尝试在推动屏幕之前获得事件锁定,ala:

synchronized(Application.getEventLock()){ 
  final UiEngine ui = Ui.getUiEngine();  
  ui.pushGlobalScreen(theScreen, 1, UiEngine.GLOBAL_MODAL);  
}  

我也只是创建一个MainScreen类型的自定义类,然后推送它而不是普通的Dialog。

答案 1 :(得分:1)

那里,那更好(现在使用代码格式化)。

public class MYSendListener implements SendListener { 
    private UiApplication _myApp; 

    public MySendListener(UiApplication myApp) { 
        _myApp = myApp; 
    } 

    public boolean sendMessage(Message m) { 
        ... 
        _myApp.requestForeground(); 
    } 
}

答案 2 :(得分:0)

在构建发送侦听器时将其放置在发送侦听器中,并在触发sendMessage时使用它。

Application.getApplication()只能获取调用线程的应用程序。