Attachments
附件
//創建一個附件處理器
首先黑莓企業服務器服務獲得所有附件連線。第三方附件處理器不能修改默認的黑莓裝置的設置。
?//引入一下類
??? net.rim.blackberry.api.mail.Message
??? net.rim.blackberry.api.mail.SupportedAttachmentPart
??? net.rim.device.api.ui.Screen
??? net.rim.device.api.ui.component.LabelField
??? net.rim.device.api.ui.component.RichTextField
??? net.rim.blackberry.api.mail.AttachmentHandlerManager
?//引入net.rim.blackberry.api.mail.AttachmentHandler接口
?//實現AttachmentHandler接口,通過黑莓所承認 MIME的接口類定義附件處理事件
?//實現字符串的支持,在下列代碼例子,如果支持附件將返回一個Boolean。
??public boolean supports(String contentType) {
???return (contentType.toLowerCase().indexOf("contenttype") != -1 ? true : false);
??}
?//當黑莓用戶選擇一個附件,使用menuString()來定義相關的菜單項字符串列表顯示的消息時。
??public String menuString() {
???return "Custom Attachment Viewer";
??}
?//使用run()來定義附件的處理,當一個黑莓裝置用戶選擇一個菜單項的消息,這個動作調用run()方法。
??在以下的例子中,我們實行代碼創建了一個全新的屏幕標題‘Attachment Viewer’與使用RichTextField
??顯示一個字符串的內容附件。
??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())));
??}
?//調用AttachmentHandlerManager.addAttachmentHandler()注冊一個附件檔。當注冊一個自定義附件處理器,
??前綴名是“x-rimdevice”的附件派和儲存的黑莓裝置。
??AttachmentHandlerManager m = AttachmentHandlerManager.getInstance();
??CustomAttachmentHandler ah = new CustomAttachmentHandler();
??m.addAttachmentHandler(ah);
??
//檢索附件的內容
?//引入net.rim.blackberry.api.mail.SupportedAttachmentPart類
?//調用SupportedAttachmentPart.getContent().
??String s = new String((byte[])p.getContent());
??
//檢索附件信息
?//引入 net.rim.blackberry.api.mail.SupportedAttachmentPart類
?//調用類SupportedAttachmentPart的方法。類SupportedAttachmentPart相當于黑莓裝置的附件觀察者。
??類UnsupportedAttachmentPart黑莓裝置沒有附件觀察者。
//發送一個帶附件的郵件
?//引入一下類
??? net.rim.blackberry.api.mail.Multipart
??? net.rim.blackberry.api.mail.SupportedAttachmentPart
??? net.rim.blackberry.api.mail.Message
??? net.rim.blackberry.api.mail.Session
??? net.rim.blackberry.api.mail.Transport
??? net.rim.blackberry.api.mail.MessagingException
?//創建Multipart類 multipart信息
??byte[] data = new byte[256]; // The attachment.
??MultiPart multipart = new MultiPart(); // Default type of multipart/mixed.
?//創建類SupportedAttachment,指定multipart的父類,在屏幕上創建每個插件
??SupportedAttachmentPart attach = new SupportedAttachmentPart( multipart,
??"application/x-example", "filename", data);
?//調用MultiPart.addBodyPart(SupportedAttachmentPart)
??multipart.addBodyPart(attach); // Add the attachment to the multipart.
?//調用Message.setContent(Multipart)與提供給類Multipart的參數設置附件內容
??msg.setContent(multipart);
?//調用Session.getTransport()和保存在一個可變對象的返回類型運輸。這個運輸對象代表了消息傳輸協議。
??Transport object represents the messaging transport protocol.
??Transport trans = Session.getTransport();
?//調用Transport.send(Message).
??try {
???trans.send(msg);
??} catch(MessagingException e) {
???System.out.println(e.getMessage());
??}
總結
以上是生活随笔為你收集整理的Attachments的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 跟我一起玩Win32开发(18):使用对
- 下一篇: 大话数据结构(个人笔记)