The BlackBerry Developer

Tuesday, June 27, 2006

Multiple Applications in one JAD

I was trying to deploy multiple applications or multiple COD files OTA with one JAD so that a user doesn't have to download each dependent project or application independently. I finally found the answer buried in a PDF that was linked to from the BlackBerry forum. You have to manually edit the jad file using the generated jad files from the jde and change your RIM-COD-URL and RIM-COD-Size properties to look like this:

RIM-COD-URL-1: mySystemProject.cod
RIM-COD-Size-1: 9972
RIM-COD-URL-2: myApplicationProject.cod
RIM-COD-Size-2: 8108

Saturday, June 17, 2006

Reset and clean the blackberry simulator

Are you sick of having a million icons on your BlackBerry simulator for every HelloWorld and demo project you have every tested? Try this to remove old programs from the simulator and start with a clean ribbon. From the command line browser to your rim jde directory, switch to the simulator sub directory and run clean.bat. This program takes longer than you would expect (about 30 seconds or so on my 3 ghz machine).

Thursday, June 15, 2006

Just the numbers


In my latest app I needed to add something to a menu that allowed a contact's phone numbers to be selected. It sounds easy enough but the whole PIM concept is foreign to me. Here is some of my code so that you can easily do this without wasting time. First create your menu item extending the net.rim.blackberry.api.menuitem.ApplicationMenuItem. Then implement your run method:


public Object run(Object context) {

...Code Here...
}

Next determine what the context is:


if (context == null) {

//Handle Null Context. This occurs in the
//Phone Menu if a call log is not selected.


}
else if (context instanceof BlackBerryContact) {

//Handle BlackBerryContect. This context is from the
//address book menu


..Code Here..
}
else if (context instanceof PhoneCallLog) {

//Handle PhoneCallLog. This context is
//from the Phone menu if a previous call is selected.


PhoneCallLog callLog = (PhoneCallLog) context;

String dialedPhoneNumber = callLog.getParticipant().getNumber();

..Do something with number..
}


The mildly confusing part is iterating over the phone numbers in a BlackBerry Contact. I did not find the APIs to be much help. The PIM class I am using is net.rim.blackberry.api.pdap.BlackBerryContact. I only mention this because there is also a class in the net.rim.blackberry.api.pim package with the same name. Here is the code:


BlackBerryContact blackBerryContact = (BlackBerryContact) context;

//Get the PIMList. It is useful for getting the labels for
//Each phone field

PIMList pimList = blackBerryContact.getPIMList();

if (blackBerryContact != null) {
//How many phone numbers does this contact have?
int phoneCount = blackBerryContact.countValues(Contact.TEL);

//Setup variables to hold the numbers and their labels.
String[] phoneNumbers = new String[phoneCount];
String[] labels = new String[phoneCount];

for (int i = 0; i > phoneCount; i++) {
//Fetch the phone number

String phoneNumber = blackBerryContact.getString(Contact.TEL, i);

//Determine the label for that number.
String label = pimList.getAttributeLabel(blackBerryContact.getAttributes(Contact.TEL, i));

//Add the number and label to the array.
phoneNumbers[i] = phoneNumber;
labels[i] = label + ":" + phoneNumber;
}

if (phoneCount == 0) {
..Handle the case when there is no number..
}
else if (phoneCount == 1) {
..Handle the number for
phoneNumbers[0]..
}
else {
//Create a dialog to ask the user which number they
//would like to use
int choice = Dialog.ask("Which Number?", labels, 0);

if (choice > -1 && choice < style="font-style: italic;">.. Handle the number for phoneNumbers[choice]..

}
else {
..Handle the case when the user doesn't pick a number..

}

}


So that is how you get the phone numbers from a blackBerry contact using J2ME. The other thing I had to look into was getting the actual name from the record. A getName() method would have been great but instead I had to use:


String firstName = c.getStringArray(Contact.NAME, 0)[Contact.NAME_GIVEN];
String lasttName = c.getStringArray(Contact.NAME, 0)[Contact.NAME_FAMILY];

Monday, June 12, 2006

OTA Deployment

This weekend I had to learn how to deploy a BlackBerry J2ME application OTA without using a BES server. It sounded easy enough. I had a COD file with my compiled code and a JAD file with the description of my application. I copied the two files out to my Apache web server. I then sent myself an email from Gmail with the link to my BlackBerry (why type when you don't have to?).

When I tried to follow the hyperlink, I received a 502 error. I could hit it fine from my machine but the BlackBerry device could not hit it. I then tried typing the URL into the BlackBerry browser. It connected and displayed the text contents of the JAD file on the screen. This was not the behavior I wanted or expected. I was hoping that the BlackBerry would just install the Java application just as it would Google Maps or any other application I had downloaded from the Web. Time to try a new approach.

I then started to research the BlackBerry Application Web Loader. I downloaded the app and read the BlackBerry Application Web Loader Developer Guide. My executive summary: this is an ActiveX control used with JavaScript for downloading BlackBerry applications and installing them from your desktop using USB synchronization. This application is not helpful for installing applications directly to your BlackBerry.

Now frustrated I decided to compare something I knew worked to my setup. When I used ieHTTPHeaders I found that the MIME Type of the JAD file should be text/vnd.sun.j2me.app-descriptor. Once I changed that and restarted my Apache service, the app downloaded and installed just fine!

The e-mail link problem:
The link I sent myself via email still wasn’t working. I realized that I started taking Gmail for granted. When I sent the URL in the email, it auto-magically created a hyperlink. When I created the link manually by creating an HTML email with an anchor tag and an href attribute, the link worked just fine.

How do I install an instant messenger?

I have recently started developing BlackBerry applications in J2ME and found that all of the BlackBerry development forums are full of people asking how to install an instant messenger on their BlackBerry. This Blog is not for people who want to install an instant messenger on their BlackBerry. This Blog is for all the problems I run into. When I find a solution, I will post it here so check back often.