Showing posts with label Blackberry. Show all posts
Showing posts with label Blackberry. Show all posts

Wednesday, 29 April 2009

Initiating a phone call in Blackberry

Lets say you wish you make your application to attempt to make a call in Blackberry. This is how you do it.

PhoneArguments pa = null;
pa = new PhoneArguments(PhoneArguments.ARG_CALL,"67838763520");
Invoke.invokeApplication(Invoke.APP_TYPE_PHONE, pa);

The bolded part is the number you wish to make a call to.

You might also want to check for any active calls before making a call. So here it is:

private static boolean gotActiveCall() {
  boolean callExist = false;
  if(Phone.getActiveCall() != null) {
    callExist = true;
  }
  return callExist;
}

BlackBerryContact Names

Getting the name of a BlackBerryContact has been been this easy... :)

This method takes in a BlackBerryContact as the argument and would return the name as a String.

private String getDisplayName(BlackBerryContact contact) {

  StringBuffer buf = new StringBuffer();
  String[] name = contact.getStringArray(BlackBerryContact.NAME, 0);

  //Get the prefix, first and last name
  boolean found = false;
  String nameseg;

  // Catch each field separately to make sure we get any available fields
  try {
   if((nameseg = name[BlackBerryContact.NAME_PREFIX]) != null && nameseg.length() != 0) {
    buf.append(nameseg);
    found = true;
   }
  } catch(IndexOutOfBoundsException ignore) {}


  try {
   if ((nameseg = name[BlackBerryContact.NAME_GIVEN]) != null && nameseg.length() != 0) {
    if(found) {
     buf.append(' ');
     buf.append(nameseg);
     found = true;
    }
   }
  } catch(IndexOutOfBoundsException ignore) {}

  try {
   if((nameseg = name[BlackBerryContact.NAME_FAMILY]) != null && nameseg.length() != 0) {
    if (found) {
     buf.append(' ');
    }
    buf.append(nameseg);
    found = true;
   }
  } catch(IndexOutOfBoundsException ignore) {}

//This is to get the Company if no names are found under the Contact
  try {
   if (found == false) {
    buf.append(contact.getString(BlackBerryContact.ORG, 0));
   }
  } catch(IndexOutOfBoundsException ignore) {}

return buf.toString();
}

BlackBerryContact Phone Numbers

Retrieving BlackBerryContact phone numbers and the labels then showing it in a dialog to let user to choose.

BlackBerryContact c;

Find out the number of phone numbers saved under a contact

c.countValues(BlackBerryContact.TEL);

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


Go through a loop to get the numbers and find out the label


for(int i = 0; i < phoneCount; i++)
{
//Fetch the phone number
String phoneNumber = c.getString(BlackBerryContact.TEL, i);

//Determine the label for that number
if(c.getAttributes(BlackBerryContact.TEL,i) == BlackBerryContact.ATTR_MOBILE)
{
label = "mobile";
}
else if(c.getAttributes(BlackBerryContact.TEL,i) == BlackBerryContact.ATTR_WORK)
{
label = "work";
}
else if(c.getAttributes(BlackBerryContact.TEL,i) == BlackBerryContact.ATTR_HOME)
{
label = "home";
}

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

}//end of for loop


Display the number in a Dialog.ask

StringBuffer sb = new StringBuffer();

//If more than one numbers found
if(phoneCount > 1)
{
  int choice = Dialog.ask("Which number to use?",labels,0);
  if (choice == 0)
  {
    sb.append(phoneNumbers[0]);
  }
  else if (choice == 1)
  {
    sb.append(phoneNumbers[1]);
  }
  else if (choice == 2)
  {
    sb.append(phoneNumbers[2]);
  }
  else if(choice == Dialog.CANCEL)
  {
    sb.append("CANCEL");
  }
}

Thursday, 19 March 2009

Add contacts in Blackberry

This code add 500 random contacts into the Blackberry phone.

ContactList blackBerryContactList = null;
try {
blackBerryContactList = (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);

//Create 500 contacts
for (int i=0;i<250;i++)
{
Contact contact = blackBerryContactList.createContact();
String[] newName = new String[blackBerryContactList.stringArraySize(Contact.NAME)];
newName[Contact.NAME_PREFIX] = "";
newName[Contact.NAME_GIVEN] = new String(new char[]{(char)i});
newName[Contact.NAME_FAMILY] = ""+i;
contact.addStringArray(Contact.NAME,Contact.ATTR_NONE,newName);
contact.commit();
}

Enumeration allContacts = blackBerryContactList.items();
m_cache = enumToVector(allContacts);
m_listField.setSize(m_cache.size());

}catch(PIMException e)
{
Dialog.alert(e.toString());
}
finally{
if(blackBerryContactList !=null){
try {
blackBerryContactList.close();
} catch (PIMException e)
{

}
}
}

Get software version in Blackberry

Here is a way to get the software version number of the Blackberry.
Copy and paste the codes in a notepad for easier reading.

import net.rim.device.api.system.*;
import net.rim.device.api.ui.UiApplication;

//Method to retrieve the Blackberry Device Software Version
public static String getSoftwareVersion()
{
String versionTxt = "";

//USING THE APPLICATION MANAGER
//(RUNNING APPS)
//get the ApplicationManager
ApplicationManager appMan = ApplicationManager.getApplicationManager();

//grab the running applications
ApplicationDescriptor[] appDes = appMan.getVisibleApplications();

//check for the version of a standard
//RIM app. I like to use the ribbon
//app but you can check the version
//of any RIM module as they will all
//be the same.
int size = appDes.length;

for (int i = size-1; i>=0; --i){
if ((appDes[i].getModuleName()).equals("net_rim_bb_ribbon_app")){
versionTxt = appDes[i].getVersion();
}
}

versionTxt = versionTxt.substring(0,3);

return versionTxt;
}


//Method to check if software version is above 4.1
//if it is 4.2 & above, return true
public static boolean isAbove41()
{
String str = getSoftwareVersion();
double versionNum = 0.0;
Dialog.alert("Software: " + str);

versionNum = Double.parseDouble(str);

double theNum = 4.1;

if (versionNum > theNum)
return true;
else
return false;

}

Monday, 24 November 2008

Can't input anything into EditField!

I created a J2ME Application for Blackberry phone and initially, I have this piece of code which is to detect when user presses the ESC button.

//When application closes
public boolean onClose() {
System.exit(0);
return true;
}

//When user press ESC key OnClose() would be called
public boolean keyChar(char key, int status, int time) {
if(key == Characters.ESCAPE){
onClose();
}
return true;
}


However, when doing this, I faced a problem of allowing the user to input anything into the EditField!!
So I just had to add this piece of line in the keyChar method to make it work :)

return super.keyChar(key, status, time);

You keyChar method should now look like this:

//When user press ESC key OnClose() would be called
public boolean keyChar(char key, int status, int time) {
if(key == Characters.ESCAPE){
onClose();
}
return super.keyChar(key, status, time);
}


Problem solved! :D