Digging into BlackBerry Hybrid development

This is the second article on the BlackBerry Hybrid development topic. In this second part we’ll look a little deeper into the Hybrid development showing common uses and samples of code. If you need an introduction on the topic, please read the first article here.

Loading a Web Page
Display an HTML page is now a very simple task:

BrowserField myBrowserField = new BrowserField();

Take a look at the BrowserFieldDemo available in any JDE installation, and discover how big is the differences with the past.

Configuring the BrowserField
The new API introduces a simple way to configure the browser field with many properties. Just invoke BrowserFieldConfig.setProperty(key, value) to override default value of a property in the BrowserField. The first parameter is the name of the property, and the second parameter is the value for that property.

For example, the following code sample set the NAVIGATION_MODE,  GEAR and COOKIES properties:

BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
BrowserField myBrowserField = new BrowserField(myBrowserFieldConfig);

Configuring the Connection Type
You can specify what type of connection layer should be used by your BrowserField in order to retrieve the web content. In normal circumstances is not required to set the connection factory, since this field reads the connection types from device default settings.

BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
ConnectionFactory connectionFactory = new ConnectionFactory();
 //Array of connection methods
int[] trasportTypes = {	TransportInfo.TRANSPORT_BIS_B,

//Set the Factory connection preferences

//Set the Factory in the configuration of the BrowserField

BrowserField myBrowserField = new BrowserField(myBrowserFieldConfig);

The creation of the ConnectionFactory class is a time consuming task, so you should put it in a separate thread when used in a real application.

Using listener
You can define a requester handler that is called just before, during and after the connection is established. For example, you can implement your own progress bar in your application using this listener. This also provide the ability to centralize the process of URL request, such as editing header fields, or any relevant debug clocking.

Any connection handler must implement the BrowserFiledListener Interface.

BrowserFieldListener listener = new BrowserFieldListener() {
 public void documentLoaded(BrowserField browserField,
                            Document document) throws Exception {
    super.documentLoaded(browserField, document);
    //Base URI of downloaded resource
    String url = document.getBaseURI();

 public void downloadProgress(BrowserField browserField,
                              ContentReadEvent event)
  throws Exception {
    super.downloadProgress(browserField, event);
    //Calculate the progress bar amount
    int progress = event.getItemsRead() / event.getItemsToRead();

Using the script engine
The Scriptable classes can be used to extend the functionalities of your java code to JavaScript. It is the base class for java objects intended to be accessed from scripts.  JavaScript code is executed in the script engine, and you can use your java code from JavaScript. This can give to you a very high control on your application functionality.

ScriptableFunction _functionVibrate = new ScriptableFunction() {
  public Object invoke(Object thiz, Object[] args)
    throws Exception {
    if(args.length > 0 ) {
      int duration = Integer.parseInt(args[0].toString());
    return UNDEFINED;

// Export the function in the Script Engine

And in your HTML code you can invoke the exported function using JavaScript:

function myFunction(duration) {

The Object values passed in and returned from the various methods on this interface should be either language-dependent primitive values, or other Scriptable objects. For ECMA-262 engines, such as, the primitive types consist of String, Integer, Double, Boolean, and Date. These will get converted to ECMA strings, numbers, boolean, and Date values. Java objects that are neither a primitive type or a Scriptable may not be correctly mapped to an object in the script environment.

6 thoughts on “Digging into BlackBerry Hybrid development

  1. Pingback: 2010 in review | Danilo Ercoli

  2. Matin Shin

    Thank you!

    I want to use pushscreen in ScriptableFunction.
    but don’t excute pushscreen in ScriptableFunction.
    Do you know?

    this source :

    ScriptableFunction nFilterInit = new ScriptableFunction()
    public Object invoke(Object thiz, Object[] args) throws Exception
    if(args.length > 0)
    System.out.println( “=====excute=====”);
    //don’t excute
    UiApplication.getUiApplication().pushScreen( new TestScreen() );

    System.out.println( “=====excute=====”);
    return Boolean.TRUE;
    browserField.extendScriptEngine(“blackberry.init”, nFilterInit);

      1. Robert Laird

        UiApplication.getUiApplication().invokeLater(new Runnable()
        public void run()
        UiApplication.getUiApplication().pushScreen( new TestScreen() );



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s