Thursday, 15 January 2009

Show Modal form from Background Application

To show a message from within a background application we can’t just use Dialog.alert(); because this will throw errors as the application is not using the Ui thread. Use the following code to launch a message/screen from the app:

Application.getApplication().invokeAndWait(new Runnable()
{
public void run()
{
UiEngine ui = Ui.getUiEngine();
Screen screen = new Dialog("Message", new Object[]{"Ok","Cancel"}, null, 0, Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION));
ui.pushGlobalScreen(screen, 1, UiEngine.GLOBAL_MODAL);

String
modalResult = ((Dialog)screen).getSelectedValue();
}
});

Can change the screen constructor depending on screen required, the above example just gives an Ok and Cancel button. Use the invokeAndWait() method blocks the current thread and waits for user to click button to give reply.

No comments:

Free Hit Counter