下面的代码让手机能通过五种方式提醒用户,是不是很酷?
1.LED Flash 灯光闪烁
2.Vibrate 震动
3.Icon - read/unread 程序图标
4.Icon with number 含动态数字的程序图标
5.Title bar with icon and number 手机屏幕上方含动态数字的图标
使用方法:在程序里面选择各个菜单进行不同的“提醒”演示。注意去查看程序图标变化的时候,不要按“返回”退出程序,免得计数器归零,正确的做法是长按“黑莓”键切换到主屏幕。
LED Flash 灯光闪烁 效果如下图:
震动的效果,就没法截图啦。
修改程序的图标,加个星号:
修改程序的图标,加未读待办的数字:
还有更酷的,在手机最上面,加新的图标和数字:
全部代码如下:
------------------------------------
package bsg.sample.ecl;
import net.rim.blackberry.api.messagelist.ApplicationIcon;
import net.rim.blackberry.api.messagelist.ApplicationIndicator;
import net.rim.blackberry.api.messagelist.ApplicationIndicatorRegistry;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.system.LED;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
public class EclScreen extends MainScreen
{
int counter = 0;
public EclScreen()
{
setTitle("Backend Alert");
//for titlebarMenu to change titlebar of homescreen
ApplicationIndicatorRegistry indicatorRegistry=ApplicationIndicatorRegistry.getInstance();
EncodedImage image=EncodedImage.getEncodedImageResource("icon/icon.JPG");
ApplicationIcon appicon=new ApplicationIcon(image);
indicatorRegistry.register(appicon, false, true);
}
/******************************************************************************************
* makeMenu(Menu,int) - creates a custom menu to add user define fields
******************************************************************************************/
protected void makeMenu(Menu menu, int instance)
{
MenuItem flashMenu = new MenuItem("flash", 100, 10) {
public void run() {
if(LED.isSupported(LED.LED_TYPE_STATUS)) {
//LED.setState(LED.STATE_ON);
//要关闭,请调用 LED.setState(LED.STATE_OFF);
//or
LED.setState(LED.STATE_BLINKING);
LED.setConfiguration(100, 1000, LED.BRIGHTNESS_100);
}
}
};
menu.add(flashMenu);
MenuItem stopflashMenu = new MenuItem("stop flash", 100, 10) {
public void run() {
if(LED.isSupported(LED.LED_TYPE_STATUS)) {
LED.setState(LED.STATE_OFF);
}
}
};
menu.add(stopflashMenu);
MenuItem viberateMenu = new MenuItem("viberate 5 seconds", 100, 10) {
public void run() {
//viberate for 10 seconds
net.rim.device.api.system.Alert.startVibrate(5*1000);
}
};
menu.add(viberateMenu);
MenuItem readMenu = new MenuItem("change icon to read", 100, 10) {
public void run() {
Bitmap icon=Bitmap.getBitmapResource("icon/read.gif");
net.rim.blackberry.api.homescreen.HomeScreen.updateIcon(icon);
}
};
menu.add(readMenu);
MenuItem unreadMenu = new MenuItem("change icon to unread", 100, 10) {
public void run() {
Bitmap icon=Bitmap.getBitmapResource("icon/unread.gif");
net.rim.blackberry.api.homescreen.HomeScreen.updateIcon(icon);
}
};
menu.add(unreadMenu);
MenuItem unreadnumberMenu = new MenuItem("change icon to unread number", 100, 10) {
public void run() {
counter++;
Bitmap icon=Bitmap.getBitmapResource("icon/unread.gif");
icon = getUpdateIconBitmap(icon, counter);
net.rim.blackberry.api.homescreen.HomeScreen.updateIcon(icon);
}
};
menu.add(unreadnumberMenu);
MenuItem titlebarMenu = new MenuItem("title bar with number", 100, 10) {
public void run() {
counter++;
ApplicationIndicatorRegistry indicatorRegistry=ApplicationIndicatorRegistry.getInstance();
ApplicationIndicator indicator=indicatorRegistry.getApplicationIndicator();
indicator.setValue(counter);
Bitmap icon=Bitmap.getBitmapResource("icon/unread.gif");
icon = getUpdateIconBitmap(icon, counter);
net.rim.blackberry.api.homescreen.HomeScreen.updateIcon(icon);
}
};
menu.add(titlebarMenu);
}
private Bitmap getUpdateIconBitmap(Bitmap bmp, int count) {
int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap iconBmp = new Bitmap(width, height);
Graphics g = new Graphics(iconBmp);
XYRect rect = new XYRect(0, 0, width, height);
g.drawBitmap(rect, bmp, 0, 0);
g.setFont(g.getFont().derive(Font.BOLD, 20, Ui.UNITS_px,
Font.ANTIALIAS_STANDARD, Font.COLORED_OUTLINE_EFFECT));
String text = Integer.toString(count);
g.setColor(Color.RED);
g.drawText(text, 0, 0);
return iconBmp;
}
}
-----------------------------------------------
package bsg.sample.ecl;
import net.rim.device.api.ui.*;
class ECLApplication extends UiApplication {
/******************************************************************************************
* main() - controls the startup of the application...thread will start in the background
* and the GUI starts when user clicks from main menu
******************************************************************************************/
public static void main(String[] args) {
ECLApplication theApp = new ECLApplication();
theApp.enterEventDispatcher();
}
/******************************************************************************************
* Constructor:
* builds the GUI and displays the fields
******************************************************************************************/
public ECLApplication() {
EclScreen screen = new EclScreen();
pushScreen(screen);
}
}
下面3个icon放到src/icon目录,分别是icon.JPG,read.gif,unread.gif