Thursday 26 March 2009

StringOptimizer in J2ME

J2ME does not provide the StringOptimizer so you would probably need to create a class of your own. But don't fret! Someone has already done that and you can take it! :D

You could download from here: http://mobilepit.com/09/using-stringtokenizer-in-j2me-javame-applications.html.

=)

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;

}

Wednesday 18 March 2009

Pause Sequence

Different J2ME phone platforms have different pause sequence so here it is:

Nokia: p (Small p)
Sony Ericsson: ;postd=p
Motorola: p (Small p)
Samsung: P (Capital P)
LG: P (Capital P)
Blackberry: , (comma)

Monday 16 March 2009

Message Formating J2ME

Java has got a MessageFormat(but not J2ME) class to replace arguments in text.

For example, “You have received {1} new {0} messages” can be formatted by each client by providing “3” as first argument and “bubble” as second one.. To learn more about it, just Google it.

Now, the problem I faced was that MessageFormat is not supported in J2ME. Therefore, I created my own class to format those strings.

Please copy and paste the code in a notepad for you to see it clearly. Enjoy! ;)

import java.util.*;
import java.io.*;
import java.lang.*;

public class Formatter{

public String format(String text,String [] arguments){

if(text != null) {
StringBuffer buf = new StringBuffer();
int start = text.indexOf("{");
int end = text.indexOf("}",start);

if (start < 0 || end <= start || args == null || args.length == 0 ) return text;

buf.append(text.substring(0,start));

while (start >= 0 && end > start) {
try {
String replaceWith = args[Integer.parseInt(text.substring(start+1,end))];
buf.append(replaceWith != null ? replaceWith : "");
} catch (Exception e){
buf.append(text.substring(start,end+1));
}
start = text.indexOf("{",end);
buf.append(start >= 0 ? text.substring(end+1,start) : text.substring(end+1));
end = text.indexOf("}",start);
}
return buf.toString();
} else {
return ("?" + text + "?");
}
}