定义一个相关菜单项字符串
在消息列表中,当用户选择一个附件时,menuString()的实现返回一个显示的菜单项字符串.
|
public String menuString() {
return "Custom Attachment Viewer";
}
|
定义附件处理
run()的实现完成了对附件的合理处理,并将附件显示给用户.
|
public void run(Message m, SupportedAttachmentPart p) {
// Perform processing on data.
Screen view = new Screen();
view.setTitle(new LabelField("Attachment Viewer"));
view.add(new RichTextField(new String((byte[])p.getContent())));
}
|
注: 当在消息列表里选择相应的菜单项时,调用run()方法.
注册一个附件处理器
AttachmentHandlerManager类控制附件是如何在BlackBerry设备上处理的.当用户打开一个相关类型的附件时,为了使消息应用程序调用你的自定义附件,调用addAttachmentHandler()注册你的附件处理器.
注:BES附件服务在接收附件时有第一优先级,第三方附件处理器不能覆盖缺省BlackBerry设备的行为,为获得更多信息,参看”BES维护和疑难解答指南”.
|
AttachmentHandlerManager m = AttachmentHandlerManager.getInstance();
CustomAttachmentHandler ah = new CustomAttachmentHandler();
m.addAttachmentHandler(ah);
|
获取附件
SupportedAttachmentPart类代表了一个BlackBerry设备上对应查看器的附件.在BlackBerry设备上.,一个没有查看器的附件作为一个UnsupportedAttachmentPart表现.
获取附件内容
调用getContent().
|
String s = new String((byte[])p.getContent());
|
获取附件信息
SupportedAttachmentPart类提供多个方法获取附件信息.下面的例子调用getName()和getSize()获取附件名和大小.
|
public void run(Message m, SupportedAttachmentPart p) {
...
String name = p.getName();
int size = p.getSize();
}
|
发送一个带有附件的消息
通过创建一个新的Multipart对象创建一个多部分信息.为了创建每个附件组件,创建一个SupportedAttachmentPart的对象,把Multipart作为它的父组件.为了把每个SupportedAttachmentPart增加到Multipart,调用对象上的addBodyPart(SupportedAttachmentPart).
当调用Message对象的setContent(Multipart)时,将 Multipart对象作为参数传入.
|
MultiPart multipart = new MultiPart(); // Default type of multipart/mixed.
SupportedAttachmentPart attach =
new SupportedAttachmentPart( multipart, "application/x-example", "filename", data);
multipart.addBodyPart(attach); // Add the attachment to the multipart.
msg.setContent(multipart);
Transport.send(msg); // Send the message.
|
- Last Updated:2007年1月23日
- Last Updated:2006年4月28日