Saturday, February 21, 2009

How To - Display a PopupScreen from a SendListener


The SendListener interface can be triggered by a foreground application that displays a GUI (such as the Messages application or a third-party application) or by a background application or process that does not contain a GUI (such as a third-party application or when appointment invitations are sent from the Calendar application). Each case requires a different approach to display a PopupScreen to the BlackBerry smartphone user, resulting in the SendListener detecting what type of application is triggering it and displaying the PopupScreen appropriately. Failing to do so can result in the PopupScreen not being shown to the BlackBerry smartphone user.

The following code example demonstrates how to detect the type of application triggering the SendListener and how to display a Dialog for each case.

public class MySendListener extends Application implements SendListener
{
public boolean sendMessage(Message msg)
{
Dialog myDialog = new Dialog(Dialog.D_OK,
"This is a Message from a 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();
}
else
{
//The sendMessage method is being triggered from
// within an application (background application).
Ui.getUiEngine().pushGlobalScreen( myDialog, 1,
UiEngine.GLOBAL_MODAL );
}
return true;
}
}

No comments:

Post a Comment

Place your comments here...