Monday, March 30, 2009

How To : Add an ApplicationMenuItem to BlackBerry Maps

0 comments

How To The BlackBerry Maps application allows the BlackBerry device user to view maps and receive directions on a BlackBerry device. BlackBerry Maps can also provide a visual route and navigational instructions to a specified destination.

BlackBerry Maps works with BlackBerry Device Software 4.1 and later. BlackBerry Maps is included with BlackBerry Device Software 4.2 and later. For use with BlackBerry Device Software 4.1, BlackBerry Maps can be downloaded from www.blackberry.com/maps.

With third-party applications, an ApplicationMenuItem can be added to the menu of BlackBerry Maps and pass a MapView object into the application.

This article focuses on adding an ApplicationMenuItem to BlackBerry Maps. Please see Here for information on the way to add a custom menu item to other BlackBerry device applications.

An ApplicationDescriptor is required for adding an ApplicationMenuItem to BlackBerry Maps. The code for registering a menu item with the BlackBerry Maps application is as follows:

 

ApplicationMenuItemRepository amir =
    ApplicationMenuItemRepository.getInstance();
ApplicationDescriptor ad_startup =
    ApplicationDescriptor.currentApplicationDescriptor();
ApplicationDescriptor ad_gui =
    new ApplicationDescriptor(ad_startup, "gui", null);
amir.addMenuItem(ApplicationMenuItemRepository.MENUITEM_MAPS,
new MapMenuItem(),ad_gui);

The menu item MapMenuItem() is then registered with the BlackBerry Maps application (MENUITEM_MAPS) to invoke the current application with ad_gui as the ApplicationDescriptor. The MapMenuItem()method contains the actual code for specifying the menu item as shown below. MapMenuItem implements the abstract class ApplicationMenuItem and implements the run and toString methods. The toString method specifies the text to display on the menu item.

private static class MapMenuItem extends ApplicationMenuItem {
     MapMenuItem() {
        super(20);
     }
     public String toString() {
        return "My Menu Item";
     }
     public Object run(Object context) {
        mv = (MapView)context;
        if (mv != null) {
            System.out.println("Latitude = " + mv.getLatitude());
            System.out.println("Longitude = " + mv.getLongitude());
            System.out.println("Zoom = " + mv.getZoom());
        }
        else {
             throw new IllegalStateException("Context is null, expected a MapView instance");
        }
        return null;
     }
}

Friday, March 27, 2009

How to : Create a custom layout manager for a screen

3 comments

How To You can create your own custom layout manager by extending the net.rim.device.api.ui.Manager class and customizing it to fit your needs. Custom layout managers enable you to customize the placement of fields on the screen and customize navigation within these fields. To do this, you need to implement at least the following three methods:

  1. public int getPreferredWidth()

This method returns the manager's preferred width.

  1. public int getPreferredHeight()

This method returns the manager's preferred height.

  1. public void sublayout(int height, int width)

This method is invoked by the manager's layout() method and instructs each child field how and where to be laid out.

This can be accomplished by calling Manager.setPositionChild(Field field, int x, int y) and LayoutChild(Field field, int width, int height) for each field contained in the manager.

Here is an example of how a screen can associate itself with a custom layout manager:

class LayoutManager extends Manager {
   public LayoutManager() {
       //construct a manager with vertical scrolling
       super(Manager.VERTICAL_SCROLL);
   }
   //overwrite the nextFocus method for custom navigation
   protected int nextFocus(int direction, boolean alt) {
       //retrieve the index of the current field that is selected
       int index= this.getFieldWithFocusIndex();
       if(alt) {
           if(direction 0){...}
               else{...}
       }
      // if we did not handle it, let the manager's parent class
      if (index == this.getFieldWithFocusIndex())
          return super.nextFocus(direction, alt);
      else
          return index;
   }
   protected void sublayout(int width, int height) {
       Field field;
       //get total number of fields within this manager
       int numberOfFields = getFieldCount();
       int x = 0;
       int y = 0;
       for (int i = 0;i < numberOfFields;i++) {
           field = getField(i); //get the field
           setPositionChild(field,x,y); //set the position for the field
           layoutChild(field, width, height); //lay out the field
           y += ...;
           ...
       }
       setExtent(width, height);
   }
   public int getPreferredWidth() {
       return 160;
   }
   public int getPreferredHeight() {
       int height= 0;
       int numberOfFields= getFieldCount();
       for (int i= 0; i < numberOfFields; i++) {
           height+= getField(i).getPreferredHeight();
           return height;
       }
   }

the main class :

   ...
   RichTextField myField = new RichTextField("Hello");
   RichTextField myOtherField = new RichTextField("World");
   LayoutManager myManager = new LayoutManager();
   MainScreen myScreen = new MainScreen();
   myScreen.add(myManager);
   myManager.add(myField);
   myManager.add(myOtherField);
   ...

Tuesday, March 24, 2009

Code : ListField with check boxes

5 comments

Code The default implementation of a ListField on a BlackBerry device will display text in a number of selectable rows. It is possible to expand on this behaviour and allow an application to add a series of check boxes to the list. This can provide a visual indication to the user of rows that have some significance to the application. In the attached sample application, check boxes are added to the left side of a ListField. The boxes can be checked and unchecked by highlighting the appropriate row and by either pressing the SPACEBAR or selecting Change Option from the menu. Here is a screenshot of what the list looks like with the first row highlighted on a BlackBerry 7290 Wireless Handheld™ with BlackBerry Device Software 4.1.


To download the source code of this application, see the CheckboxListField.zip file.

Monday, March 23, 2009

Code : End incoming call programatically

2 comments

Code Event injector will work only when the keystrokeinjection permission is set to allow for that particular application. To do so you have to go to the Options -> Advanced Options -> Applications –> Press Menu key –>  Modules –> select your application –> Press Menu key –> Edit Permissions –> Expand Interaction option-> set to allow keystrokeinjection permission.

EventInjector.KeyCodeEvent pressEndKey = new
    EventInjector.KeyCodeEvent(
    KeyCodeEvent.KEY_DOWN,( char)
    Keypad.KEY_END,0,100);

EventInjector.KeyCodeEvent releaseEndKey =
    new EventInjector.KeyCodeEvent(
    KeyCodeEvent.KEY_UP,(char)
    Keypad.KEY_END,0,100);

Thread.sleep(1000);
EventInjector.invokeEvent(pressEndKey);
EventInjector.invokeEvent(releaseEndKey);

Saturday, March 21, 2009

Code : Change HomeScreen-Icon on an external event

0 comments

Code Code:

String[] newargs = {"homescreen"};
int modHandle = CodeModuleManager
    .getModuleHandle("HomeScreenIconTest");
ApplicationDescriptor[] apDes =
    CodeModuleManager.getApplicationDescriptors(modHandle);
ApplicationDescriptor apDes2 = new
    ApplicationDescriptor(apDes[0], newargs);
ApplicationManager.getApplicationManager()
    .runApplication(apDes2);

and the main from the Class HomeScreenIconTest:

Code:

public static void main(String[] args) {
    if (args.length > 0 && args[0].equals("homescreen")) {
        HomeScreen.updateIcon(CROSS,0);
        HomeScreen.setRolloverIcon(MAN,0);
    } else {
        HomeScreenTest theApp = new HomeScreenTest();
        theApp.enterEventDispatcher();
    }
}

If event listener (running as a system service) registers
an event then now update the icon of main application,
indicating that there is an event waiting.
Maybe draw a number with graphics on the Bitmap.

How To : Auto-launch midlets using pushregistry except incoming message

2 comments

How To Launch midlets using pushregistry other than incoming message through alarm....its very simple ...all you need to do is just register
alarm for a particular time for launching the midlet automatically...
below is the syntax for registering the alarm :

Date alarm = new Date();
String midletClassName = this.getClass().getName();
PushRegistry.registerAlarm(midletClassName, alarm.getTime());

pushregistry.registeralarm method used to register your midlet.

Code:



PushRegistry.registerConnection(defaultURL,midletClassName,
defaultFilter);



Enjoy!

Code : Invoke rich media explorer

0 comments

Code /* Application.java */

import net.rim.device.api.ui.UiApplication;
public class Application extends UiApplication{

    public static void main(String[] args) {
        (new Application()).enterEventDispatcher();
    }

    public Application() {
        this.pushScreen(new MyScreen());
    }
}

/* MyScreen.java */

import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.ApplicationManagerException;
import net.rim.device.api.system.CodeModuleManager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.system.Bitmap;

public class MyScreen extends MainScreen {

    private static final MenuItem menuItemClose =
                                                 new MenuItem(
                                                             "Close", 13, 244) {
        public void run() {
            System.exit(0);
        }
    };

    private static final MenuItem menuItemRun =
                                                 new MenuItem(
                                                               "Run", 13, 244) {
        public void run() {
            int handle = CodeModuleManager
                                .getModuleHandle("net_rim_bb_file_explorer");
            if (handle <= 0) {
                System.out.println("HANDLE IS INVALID");
                return;
            }
            ApplicationDescriptor[] appDescriptors = CodeModuleManager
                                            .getApplicationDescriptors(handle);
            if ((appDescriptors==null) || (appDescriptors.length == 0)) {
                System.out.println("App descriptors are missing");
                return;
            }
            String[] args = {"", "" };
            ApplicationDescriptor descriptor = new ApplicationDescriptor(
                                      appDescriptors[0], "Media explorer", args,
                                      Bitmap.getBitmapResource("icon.png"), -1, null, -1,
                                      ApplicationDescriptor.FLAG_SYSTEM);           
            try {
                ApplicationManager.getApplicationManager().runApplication(descriptor);
            } catch (ApplicationManagerException e) {
                System.out.println("App cannot be launched");
                System.out.println(e.getMessage());
                return;               
            }       
        }
    };

    public MyScreen() {
        super();
    }

    public boolean onClose() {
        System.exit(0);
        return false;
    }

    public void makeMenu(Menu menu, int context) {
        menu.add(menuItemRun);
        menu.add(menuItemClose);
    }
}

Enjoy!

Thursday, March 19, 2009

How To : Format text in a RichTextField

6 comments

How To A RichTextField can be used to present selectable, formatted text to a user. Multiple fonts, font sizes, and font formatting (for example, Bold, Italic, Underlined) can be used in a single RichTextField.

 

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
public final class RichTextFieldSample extends UiApplication
{
    public static void main(String[] args)
    {
            RichTextFieldSample theApp = new RichTextFieldSample();
            theApp.enterEventDispatcher();
    }
    public RichTextFieldSample()
    {
        //Define the screen.
        MainScreen mainScreen;
        //Defint the RichTextField.
        RichTextField rtField;
        //An array of fonts to be passed to the RichTextField.
        Font fonts[] = new Font[3];
        //A list of offsets of character positions of the string contained
        //in the RichTextField where font appearance will change.
        //The first value in the offset array should be 0,
        //the starting character of the string and the last value should
        //be the last character position in the string.
        int[] offset = new int[4];
        //An array of indexes that align with the offset values and indicate
        //the index of the font array of the font to use starting at an offset.
        byte[] attribute = new byte[3];
        //Get three instances of the default font.
        //On plain, one bold and one bold and italic.
        fonts[0] = Font.getDefault();
        fonts[1] = Font.getDefault().derive(Font.BOLD);
        fonts[2] = Font.getDefault().derive(Font.BOLD | Font.ITALIC);
        //The string of text we will display in the RichTextField.
        String richText = "This is how you create text with formatting!!!";
        //Offset will start a character 0 of the string.
        offset[0] = 0;
        //At character position 0 we want to use the font at
        // position 2 of the font array (bold and italic).
        attribute[0] = 2;
        //Change to the default (non-bold, non-italic) font at
        //the 4th character position.
        offset[1] = 4;
        attribute[1] = 0;
        //Change to bold font at the 33rd character position.
        offset[2] = 33;
        attribute[2] = 1;
        //Add the last character position to the offset.
        offset[3] = richText.length();
        //Instantiate mainScreen.
        mainScreen = new MainScreen();
        //Set the title of mainscreen.
        mainScreen.setTitle(new LabelField
          ("RichTextFieldSample Sample",
            LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));
        //Instantiate rtField.
        rtField = new RichTextField
          (richText, offset, attribute, fonts,
           RichTextField.USE_TEXT_WIDTH);
        //Add the RichTextField to the MainScreen.
        mainScreen.add(rtField);
        //Push the MainScreen onto the stack.
        pushScreen(mainScreen);
    }
}