Monday, March 16, 2009

Programming for BlackBerry with Java ME

Research in Motion BlackBerry phones support the Java ME standard, so for a Java ME developer is easy to get started on BlackBerry. But a few things do not work as expected...

Menu
The BlackBerry has a very consistent user interface, with a menu key and a back key. These keys can not be catched by your Java ME canvas, pressing them will not call the keyPressed() method as you might have expected. Instead the menu key will always open the menu. If you have not added Java ME commands to the Canvas, the BlackBerry will create a menu itself, with 'Close' as the only alternative. The back key will trigger the command with the command type EXIT if there is one and do nothing is there is no such command.

This basically means that you should always use Java ME Commands in yor BlackBerry application. There is no other way within the Java ME API to get the BB keys to work as expected.

Other keys
There are no Nokia-style softkeys on a BlackBerry keyboard. But there is a trackball (trackwheel on older models) and a qwerty keyboard.

The trackwheel sends Canvas UP, DOWN, RIGHT and LEFT codes and trackball press can also be handled and expected. Your application should probably use these keys a lot. Key repeat is however not supported, so if the user scrolls the trackball you will get a series of keyPressed events and no keyRepeated.

The qwerty keys will call keyPressed with codes as expected, you can use getKeyName. Just remember that some models have a smaller keyboard, with two characters per key. You will always get the first character.

The return, backspace and space keys will also deliver key events as expected. The shift and alt keys will however not. Instead they affect the keycode delivered to the application. That is ok for the qwerty keys, but also it means that you can not tell if the user is holding down shift and scrolling or pressing the trackball.

Fullscreen mode
You Java ME Canvas will always cover the full screen. The call to setFullScreenMode has no effect. This also means that the sizeChanged method will never be called. So you should make sure that your application does not depend on it beging called.

The conclusion is that the BlackBerry Java ME implementation is a good one, and can be very well be used as a base for your implementation on BlackBerry. But if your midlet is based on the Canvas class and developed for Nokia-style phones you will probably need to make some changes, mainly with menues, softkeys etc. You can them detect at runtime if the midlet is on a BlackBerry or not and adjust to that.

10 comments:

Yariv said...

I don't think the back key triggers anything in J2ME, It's not triggering any type of command.
are you sure you succeeded using the back key in J2ME?

Erik said...

By the back key I mean the key beside the trackbal, with an arrow that bends and goes backward. In BlackBerry documentastion it is sometimes called the escape key, perhaps that is a better name.
I believe it actually triggers Command.CANCEL if there is such a command, if not it triggers Command.BACK if there is one, if not it triggers Command.EXIT.

scotty said...

Hi, I'm trying to get into java / blackberry programming and have found your blog to be very informative. I was wondering if you could help as you seem to be quite knowledgeable in the area. I'm trying to modify the interface of a java program written for nokia s40 and update it to work with a blackberry. I'm pretty new at this so any advice would be appreciated! Cheers.

Erik said...

Hi, scotty! Sure, just post your questions and I'll try to answer them.

mobileappmax said...

wow,its great implementation on

blackberry java ME.

Avisekhar said...

The "Back" key triggers keyPressed() method in J2ME. Its tested. The only issue with this is with the unusual key code (not in ASCII set) it throws each time it is pressed."Menu" key does the same as well.

Victor Essien said...

I have an issue, I used the blackberry SDK to build my mobile app. The issue is that the Left menu key and right menu keys aren't working, rather the 'q' and 'p' keys work in their place

Victor Essien said...

I used the Blackberry SDK to build my mobile app. The issue is that the right and left menu keys won't work, rather the keys 'q' and 'p' take their place. How do I resolve this?

Victor Essien said...

I used the Blackberry SDK to build my mobile app. The issue is that the right and left menu keys won't work, rather the keys 'q' and 'p' take their place. How do I resolve this?

Victor Essien said...

I used the Blackberry SDK to build my mobile app. The issue is that the right and left menu keys won't work, rather the keys 'q' and 'p' take their place. How do I resolve this?