//创建一个空白SMS信息
//调用Invoke.invokeApplication()
Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments
( MessageArguments.ARG_NEW_SMS));
//创建一个新的数据信息
使用的API javax.wireless.messaging package (JSR 120).
//引入一下接口
• javax.wireless.messaging.MessageConnection
• net.rim.plazmic.mediaengine.io.Connector
• javax.wireless.messaging.TextMessage
//创建一个短信息
MessageConnection mc = (MessageConnection)Connector.open( "sms://" );
TextMessage m = (TextMessage)mc.newMessage( MessageConnection.TEXT_MESSAGE );
m.setAddress( "sms://5558888" );
m.setPayloadText( "An SMS Message for you" );
//调用Invoke.invokeApplication()使用一下参数
• APP_TYPE_MESSAGES:常量
• MessageArguments:新的消息参数类运用文本类
Invoke.invokeApplication( Invoke.APP_TYPE_MESSAGES, new MessageArguments( m ) );
//创建一个多媒体信息
//调用Invoke.invokeApplication()
Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments
( MessageArguments.ARG_NEW_MMS));
//创建一个空白email信息
//调用Invoke.invokeApplication()
Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments
( MessageArguments.ARG_NEW));
//创建一个email信息
//创建一个email message类
Message m = new Message();
Address a = new Address("mLi@rim.com", "Ming Li");
Address[] addresses = {a};
m.addRecipients(net.rim.blackberry.api.mail.Message.RecipientType.TO, addresses);
m.setContent("A message for you...");
m.setSubject("Email for you");
//调用Invoke.invokeApplication()使用一下参数
• APP_TYPE_MESSAGES:常量参数
• MessageArguments:一个新的MessageArguments对象,使用email类
Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments(m));
//创建一个空白PIN消息
//调用Invoke.invokeApplication()
Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments
( MessageArguments.ARG_NEW_PIN));
//创建一个PIN消息
//创建一个PIN消息
Message m = new Message();
PINAddress pa = new PINAddress("ABCDEF99", "Mark Chapters");
Address[] addresses = {pa};
m.addRecipients( net.rim.blackberry.api.mail.Message.RecipientType.TO,
addresses );
m.setContent("A message for you...");
m.setSubject("PIN message for you");
//调用Invoke.invokeApplication()使用一下参数
• APP_TYPE_MESSAGES:常量参数
• MessageArguments:一个新的MessageArguments对象,使用email类PIN消息.
Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments(m));
//检索信息通知
//引入一下接口
• net.rim.blackberry.api.mail.event.FolderListener
• net.rim.blackberry.api.mail.event.StoreListener
//实现FolderListener与StoreListener接口
public class MailTest implements FolderListener, StoreListener { ... }
//捕获异常ControlledAccessException.
//添加一个监听message store
//捕获异常NoSuchServiceException.
try {
//
} catch (NoSuchServiceException e) {
System.out.println(e.toString());}
//调用Session.waitForDefaultSession().getStore()检索Store类
try {
Store store = Session.waitForDefaultSession().getStore();
} catch (NoSuchServiceException e) {
System.out.println(e.toString());
//调用store.addStoreListener()添加 StoreListener
//添加message store监听
//引入net.rim.blackberry.api.mail.Store class.
//引入net.rim.blackberry.api.mail.event.StoreListener interface.
//实现the net.rim.blackberry.api.mail.event.StoreListener接口
//实现 StoreListener.batchOperation().
void batchOperation(StoreEvent e) {
// Perform action when messages added or removed in batch operation.
}
//添加folder监听
//引入net.rim.blackberry.api.mail.event.FolderListener接口
//引入net.rim.blackberry.api.mail.event.FolderListener接口
//当接收到信息通知检索Folder类
Folder[] folders = store.list(Folder.INBOX);
Folder inbox = folders[0];
//添加类的实例FolderListener接口
inbox.addFolderListener(this);
//实现FolderListener.messagesAdded()与FolderListener.messagesRemoved().
void messagesAdded(FolderEvent e) {
// Perform processing on added messages.
}
void messagesRemoved(FolderEvent e) {
// Perform processing on removed messages.
}
//检索所有唯独email信息 messages在folders与store中
//调用Store.getUnreadMessageCount().
int numUnread = store.getUnreadMessageCount();
//查看更多的email信息内容
默认的情况下第一部分的信息发给黑莓终端
//在抽象类BodyPart创建实例类
TextBodyPart tb = new TextBodyPart(new MultiPart());
//从服务器上获取更多资料调用
TextBodyPart.hasMore()
//想确定黑莓用户更多数据调用
TextBodyPart.moreRequestSent().
//获得一个运用对象调用Session.getTransport(),并保存在一个可变对象的返回类型运输。
Transport trans = Session.getTransport();
//要获得更多信息调用Transport.more(BodyPart bp, boolean reqAll),Transport.more(BodyPart bp, boolean reqAll)
的第二个参数是一个Boolean值指定是否获取下一个section(false)或剩下的部分(true).
if (( tb.hasMore() ) && (! tb.moreRequestSent()) {trans.more(tb, true);}
------------------------------------------------------------------------------------------------------------
//检索email文件信息的内容
private void readEmailBody(TextBodyPart tbp)
//返回邮箱内容的字符串
_plainTextMessage = (String)tbp.getContent();
//坚定TextBodyPart在服务器上是否可用
if (tbp.hasMore() && !tbp.moreRequestSent())
{
try
{
Transport.more((BodyPart)tbp, true);
}
catch (Exception ex)
{
Dialog.alert("Exception: " + ex.toString());
}
}
}
//在email信息中检索HTML内容
private void readEmailBody(MimeBodyPart mbp)
{
//Extract the content of the message.
//检索MimeBodyPart里的内容
Object obj = mbp.getContent();
String mimeType = mbp.getContentType();
String body = null;
//黑莓手机能可以将HTML转化为字符串
if (obj instanceof String)
{
body = (String)body;
}
//黑莓不可以将HTML转化为字符串
else if (obj instanceof byte[])
{
body = new String((byte[])obj);
}
if (mimeType.indexOf(ContentType.TYPE_TEXT_PLAIN_STRING) !=
-1)
{
_plainTextMessage = body;
//Determine if all of the text body part is present.
if (mbp.hasMore() && !mbp.moreRequestSent())
{
try
{
//检索其余数据
Transport.more((BodyPart)mbp, true);
}
catch (Exception ex)
{
Dialog.alert("Exception: " + ex.toString());
}
}
}
else if (mimeType.indexOf(ContentType.TYPE_TEXT_HTML_STRING) !=
-1)
{
_htmlMessage = body;
//Determine if all of the HTML body part is present.
RIM Confidential and Proprietary Information - Beta Customers Only. Content and software are subject to change.
if (mbp.hasMore() && !mbp.moreRequestSent())
{
try
{
Transport.more((BodyPart)mbp, true);
}
catch (Exception ex)
{
Dialog.alert("Exception: " + ex.toString());
}
}
}
}
}
-----------------------------------------------------------------------------------------------------------
//Open a message
//调用Session.waitForDefaultSession.getStore()检索message store
Store store = Session.waitForDefaultSession.getStore();
//调用Store.getFolder()检索folder
Folder folder = Store.getFolder("SampleFolder");
//调用folder.getMessages()检索message objects与 store the message objects在Message队列,循环访问 与检索信息,
//比如sender与subject
Message[] msgs = folder.getMessages();
//从列表中查询出信息,调用Message函数从message对象中检索出适当的领域显示在Blackberry平台上
Message msg = msgs[0]; // Retrieve the first message.
Address[] recipients = msg.getRecipients(Message.RecipientType.TO)
Date sent = msg.getSentDate();
Address from = msg.getFrom();
String subject = msg.getSubject();
Object o = msg.getContent();
RIM Confidential and Proprietary Information - Beta Customers Only. Content and software are subject to change.
// Verify that the message is not multipart.
if ( o instanceof String ) {
String body = (String)o;}
//调用Message.getBodyText()从message中检索信息, 如果返回内容不是纯文本内容,函数返回true。
//Notify a BlackBerry device application that an email message is about to be sent
//实现SendListener接口
public class MailSendListener implements SendListener{
//创建一个类的实例实现SendListener接口
MailSendListener mailSL = new mailSendListener();
//调用Session.waitForDefaultSession().getStore()检索Store对象
try {
Store store = Session.waitForDefaultSession().getStore();
}
//捕获NoSuchServiceException异常.
catch (NoSuchServiceException e) {
System.out.println(e.toString());
}
//调用Store.addSendListener(MailSendListener)添加SendListener实例
store.addSendListener(mailSL);
//Notify a BlackBerry device application that an SMS message with multimedia is about to be sent
//实现SendListener接口
public class MMSSendListener implements SendListener{
//创建类的实例实现SendListener接口
MMSSendListener mmsSL = new mmsSendListener();
//添加SendListener实例
MMS.addSendListener(mmsSL);
//Send a message
//创建Message对象
Message msg;
//指定folder保存发送的message.
Store store = Session.getDefaultInstance().getStore();
Folder[] folders = store.list(Folder.SENT);
Folder sentfolder = folders[0];
msg = new Message(sentfolder);
//创建Address对象
Address toList[] = new Address[1];
//添加address到array
try {
toList[0]= new Address("clyde.warren@blackberry.com", "Clyde Warren");
}
//捕获AddressException异常
catch(AddressException e) {
System.out.println(e.toString());
}
//调用Message.addRecipients()提供类容器recipient (TO, CC, or BCC)与addresses数组参数给函数
//如果邮件有多个收件人,调用 Message.addRecipients()遍历recipient容器类
msg.addRecipients(Message.RecipientType.TO, toList);
//调用Message.setFrom(Address).
Address from = new Address("clyde.warren@blackberry.com", "Clyde Warren");
msg.setFrom(from);
//调用Message.setSubject(String).
msg.setSubject("Test Message");
//调用Message.setContent(String.Typically,从黑莓设备中进行检索
try {
msg.setContent("This is a test message.");
} catch(MessagingException e) {
System.out.println(e.getMessage());
}
//调用Session.getTransport()返回store对象,The 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());
}
//回复信息
//调用Session.getTransport()
try {
trans.send(msg);
} catch(MessagingException e) {
System.out.println(e.getMessage());
}
Transport trans = Session.getTransport();
//调用Session.waitForDefaultSession().getStore()检索Store类
Store store = Session.waitForDefaultSession().getStore();
//调用Store.list(INBOX)检索folders在收件箱,储存信件的文件夹Folder array.
Folder[] folders = store.list(INBOX);
//制定一个特殊的数组去检索文件夹
Folder inbox = folders[0];
//调用Folder.getMessages()在文件夹中检索信息
Message[] messages = inbox.getMessages();
//调用Message.reply(Boolean)指定为true回复所有收件人或者为false只回复发件人
if( messages.length > 0 ) {
Message msg = messages[0];
}
Message reply = msg.reply(true);
//转发信息
//调用Message.forward()在Message对象
Message fwdmsg = msg.forward();
//创建Address数组
Address toList[] = new Address[1];
//添加新的邮件地址到数组
//调用Message.addRecipients(int, Address[])添加到收件人的Message中
toList[0]= new Address("clyde.warren@blackberry.com", "Clyde Warren");
fwdmsg.addRecipients(Message.RecipientType.TO, toList);
//调用Message.setContent(String)将邮件内容添加打Message中
try {
fwdmsg.setContent("This is a forwarded message.");
} catch(MessagingException e) {
System.out.println(e.getMessage());
}
//调用Session.getTransport() and store the returned object in a variable of type Transport. The
Transport object represents the messaging transport protocol.
Transport trans = Session.getTransport();
//调用Transport.send(Message).
try {
trans.send(fwdmsg);
} catch(MessagingException e) {
System.out.println(e.getMessage());
}
//操作文件夹
//调用Session.waitForDefaultSession().getStore()检索store.
Store store = Session.waitForDefaultSession().getStore();
//部分函数
//打开文件夹
a.调用Store.list()检索文件夹列表
Store store = null;
store = Session.waitForDefaultSession().getStore
();
Folder[] folders = store.list();
b.调用Invoke.invokeApplication()使用APP_TYPE_MESSAGES常量参数
Invoke.invokeApplication
(Invoke.APP_TYPE_MESSAGES,
new MessageArguments( folders[0]));
列出store中的邮件列表
Invoke Store.list().
Folder[] folders = store.list();
一类检索folders中文件数组
Invoke Store.list(int) and provide as a parameter the folder type.
Folder[] folders = store.list(INBOX);
Folder inbox = folders[0];
检索数组folders通过search.
Store.findFolder(String).
Folder[] folders = store.findFolder("Inbox");
通过文件名检索folder
a.调用Store.getFolder(String)取出规定参数与文件夹的绝对路径
Folder folder = store.getFolder("Mailbox - Aisha Wahl/Inbox/Projects");
b. 创建代码管理ID检索不到文件夹的异常
通过ID检索文件夹信息
a.调用Folder.getID()检索folder ID.
b.调用Store.getFolder()将ID作为一个参数传入
Folder[] folders = store.list();
long id = folders[0].getId();
Folder f2 = store.getFolder(id);
文件信息
调用Folder.appendMessage(Message)在Folder类
Message msg = new Message();
//...
Folder folder = store.getFolder("Inbox");
folder.appendMessage(msg);