tags:

views:

95

answers:

3

I have an app that is listening in background and when the user clicks "send" it displays a dialogue. However I need to bring my app to foreground so the user answers some questions before letting the message go. but I haven't been able to do this, this is the code in my 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 is an object I created above:

Application App = Application.getApplication();

I have also tried to invoke the App to foreground using its processID but so far no luck.

A: 

i have managed to achieve something similar to what you're describing but the difference is, my dialogs are displayed asynchronously, which might actually be easier... so in your case..

the first i could suggest you try is get the event lock before pushing the screen, ala:

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

I would also just create a custom class of type MainScreen and push that instead of plain Dialog.

bryanallott
if I push the screen the way you proposed here, would that be better than the pushing a MainScreen? or is that only 2 ways of achieving the same result?
Luis Armando
theScreen variable is of type MainScreen or PopupScreen (i've used both successfully).
bryanallott
I created a UiScreen uiscreen = new UiScreen(); (that extends MainScreen) and did this inside the sendListener:synchronized(Application.getEventLock()){ final UiEngine ui = Ui.getUiEngine(); ui.pushGlobalScreen(uiscreen, 1, UiEngine.GLOBAL_MODAL); }I got an Illegal Argument Exception =/ did I miss something? is there a way we could talk about it a bit more? it's a school project and I'm about to reach the deadline =/
Luis Armando
i've uploaded simple skeleton: http://drop.io/2rkddp3 to take a look at. hopefully that helps.
bryanallott
A: 

Cache your app instance inside your send listener when you construct it, and use that when sendMessage is fired.

Application.getApplication() only gets you the app of the calling thread.

Bradley
how do I Cache my app then? (I'll keep on Googling for an answer)
Luis Armando
A: 

There, that's better (now with code formatting).

public class MYSendListener implements SendListener { 
    private UiApplication _myApp; 

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

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