Create an attachment handler

You can use the AttachmentHandler interface to manage an attachment to an email message that appears in the message list on the BlackBerry® device.

Note: The BlackBerry® Attachment Service receives all attachments first. Third-party attachment handlers cannot override this default behavior. For more information about the BlackBerry Attachment Service, see the BlackBerry Enterprise Server Administration Guide.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.mail.*;
    import net.rim.device.api.ui.container.*;
    import net.rim.device.api.ui.component.*;
  2. Implement the AttachmentHandler interface to create a custom attachment handler.
    public class AttachTest implements AttachmentHandler {...}
  3. Implement the supports(String) method, to specify the content type of the attachment supported by your handler.
    public boolean supports(String contentType)
    {
       return (contentType.toLowerCase().indexOf("contenttype") != -1 ? true : false);
    }
  4. Implement the menuString() method, to specify the text of the menu item that displays when a user selects an attachment.
    public String menuString()
    {
       return "Custom Attachment Viewer";
    }
  5. Implement the run() method to specify what should happen when a user clicks the menu item. In the following code sample, a new screen uses the RichTextField class to display a String representation of the content of the attachment.
    public void run(Message m, SupportedAttachmentPart p)
    {
       MainScreen view = new MainScreen();
       view.setTitle("Attachment Viewer");
       view.add(new RichTextField(new String((byte[])p.getContent())));
    }
  6. Invoke AttachmentHandlerManager.addAttachmentHandler(), to register the attachment handler with the manager. Note that the attachment name must be prefixed with "x-rimdevice" for the attachment to be sent and stored on the BlackBerry device.
    AttachmentHandlerManager m = AttachmentHandlerManager.getInstance();
    CustomAttachmentHandler ah = new CustomAttachmentHandler();
    m.addAttachmentHandler(ah);
Previous topic: Attachments

Was this information helpful? Send us your comments.