BlackBerry上的一大特色就是基于消息的功能,其中消息提醒功能是初学BlackBerry遇到的一大难题。BlackBerry上的消息(Messaging)定义很广,包括邮件、短信/彩信、即使消息(如MSN/FaceBook/Twitter/BlackBerry Messenger等)、以及应用程序自己定义的消息。
这些消息到达BlackBerry时,BlackBerry提供了5种消息提醒的方法,它们是:LED闪烁、提示音、界面左上方的小图标提醒(叫做Indicator),应用程序图标的变化(比如在有新消息到来时,应用程序图标的右上角会出现一个红色的星号)。
同时,BlackBerry提供一种机制,可以将所有的消息都注册到系统唯一的Message Inbox中(也就是第一排左边第一个图标),在这个Inbox中,当不同的应用有新的消息到来时,可以新添一条自己的记录(包含自定义的应用图标,自定义的打开菜单,在Message文件列表中创建自己的消息目录)。
几种提醒方式
2.1 应用程序图标
效果:
示例代码:
1, 更新应用程序图标为已读
Bitmap icon=Bitmap.getBitmapResource("icon/read.gif");
net.rim.blackberry.api.homescreen.HomeScreen.updateIcon(icon);
2, 更新应用程序图标为未读
Bitmap icon=Bitmap.getBitmapResource("icon/read.gif");
net.rim.blackberry.api.homescreen.HomeScreen.updateIcon(icon);
2.2 LED灯
效果:
示例代码:
1,首先需要import相应的API包:
import net.rim.device.api.system.*;
或者
import net.rim.device.api.system.LED;
2,关闭LED灯
if(LED.isSupported(LED.LED_TYPE_STATUS))
{
LED.setState(LED.STATE_OFF);
}
3,打开LED灯或者开启LED灯闪烁
if(LED.isSupported(LED.LED_TYPE_STATUS))
{
//LED.setState(LED.STATE_ON);
//or
LED.setState(LED.STATE_BLINKING);
LED.setConfiguration(100, 1000, LED.BRIGHTNESS_100);
}
4,注意捕获抛出的异常
如果setState(int state)的输入参数为不正确的状态时,可能会抛出异常:
2.3 震动
示例代码:
net.rim.device.api.system.Alert.startVibrate(5*1000);
2.4 提示音
调用系统声音播放的接口,示例如下:
// create an instance of the player from the InputStream
Player player = javax.microedition.media.Manager.createPlayer(path, type);
player.realize();
player.prefetch();
VolumeControl volumeControl = (VolumeControl) player.getControl("VolumeControl");
volumeControl.setLevel(100);
// start the player
player.start();
2.5 Indicator
如下图所示,BlackBerry提供Indicator的机制,可以在顶部状态栏显示一个小图标,同时还可以在小图标旁边显示一个数字,来标识是否有新消息到来以及新消息的数目。
效果:
常用的实现步骤:在系统中注册自己的图标。每来一条新的消息,将图标设为显示状态,并且将图标旁边的数字加1;当每读一条未读消息,将图标设为隐藏,并将图标旁边的数字减1.
示例代码:
第一步:创建ApplicationIndicatorRegistry和ApplicationIcon对象
ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
EncodedImage image = EncodedImage.getEncodedImageResource( "clowds.gif" );
ApplicationIcon icon = new ApplicationIcon( image );
第二步:注册或者获取ApplicationIndicator对象
//第一次调用时,通过方法ApplicationIndicator register(
ApplicationIcon icon, boolean iconOnly, boolean visible)
来注册Indicator,其中第二个参数控制是否只显示图标而不显示数字
ApplicationIndicator indicator = reg.register( icon, false, true);
//以后可以通过以下方法来获取已注册的Indicator实例。
ApplicationIndicator AppIndicator = reg.getApplicationIndicator();
第三步:操作Indicator(显示/隐藏/删除)
AppIndicator.set( newIcon, newValue );
//显示图标
ApplicationIndicator.setVisible(true).
//隐藏图标
ApplicationIndicator.setVisible(false).
//从系统中删除图标的注册信息
ApplicationIndicatorRegistry.unregister().
3. 在系统的Message Inbox中加入你的应用程序
系统的Message Inbox中是所有消息的唯一入口,包括Email,SMS/MMS,IM以及第三方应用的消息。BlackBerry提供一种机制,允许第三方应用将自己的消息写入Message Inbox,并且定制这些消息的图标,以及菜单项等。同时可以建立自己的应用程序消息目录,将自己的消息存放在这个目录下。
效果:
常用的实现步骤:分为两个步骤,第一个步骤是将你的图标和你的应用程序关联起来。在系统的Message Inbox中注册你自己应用的消息,并且为这些消息对应上你自己定制的图标,同时为这些消息定制菜单项。(如上图所示)
示例代码如下:
ApplicationIcon newIcon = new ApplicationIcon( EncodedImage.getEncodedImageResource( "ml_sample_new.png" ) );
ApplicationIcon openedIcon = new ApplicationIcon( EncodedImage.getEncodedImageResource( "ml_sample_opened.png" ) );
int MESSAGE_TYPE = 0;
reg.registerMessageIcon( MESSAGE_TYPE, STATUS_NEW, newIcon );
reg.registerMessageIcon( MESSAGE_TYPE, STATUS_OPENED, openedIcon );
通过以上步骤,当Message Inbox中的消息为未读状态时,显示的图标为newIcon;已读状态时,显示的图标为openedIcon。
第二个步骤是在BlackBerry中定义一个消息存放的应用消息目录,以及这个目录对应的消息处理函数。进入应用消息目录的方法如下:
查看根目录名的方法,选择桌面“消息”,进去后点菜单键,向下滚动选择“查看文件夹”,在目录树中就可以看到你设置的目录名了。
示例代码如下:
public class MLSampleMessage implements ApplicationMessage
· 获取 ApplicationMessageFolderRegistry 的引用
ApplicationMessageFolderRegistry reg = ApplicationMessageFolderRegistry.getInstance();
· 在BlackBerry上注册自己的应用目录,来存放来自你的应用的消息。在下面,注册了两个目录,一个是Inbox,另一个是Deleted Messages。
ReadableList inboxMessages = messages.getInboxMessages();
ReadableList deletedMessages = messages.getDeletedMessages();
ApplicationMessageFolder inboxFolder = reg.registerFolder( INBOX_FOLDER_ID, "Inbox", inboxMessages );
ApplicationMessageFolder deletedFolder = reg.registerFolder( DELETED_FOLDER_ID, "Deleted Messages",
deletedMessages, false );
deletedFolder.addListener( this , ApplicationMessageFolderListener.MESSAGE_DELETED );
public class AppFolderListener implements ApplicationMessageFolderListener
· 下一步,需要调用actionPerformed() 来处理Folder事件, 这个方法定义在接口ApplicationMessageFolderListener中.
public void actionPerformed( int action, ApplicationMessage[] messages, ApplicationMessageFolder folder ) {
// check if action was performed on multiple messages
if( messages.length == 1 ) {
switch( action ) {
case ApplicationMessageFolderListener.MESSAGE_DELETED:
messageStore.deleteInboxMessage( message );
reg.setRootFolderName( "OA List" );