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, 26 March 2009
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)
{
}
}
}
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;
}
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)
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 + "?");
}
}
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 + "?");
}
}
Friday, 20 February 2009
Finding out the different phone platforms
public static final String J2ME_PLATFORM = (System.getProperty("microedition.platform") != null ? System.getProperty("microedition.platform").toLowerCase() : "");
//Method to check if the phone is using Sony Ericsson platform
public static final boolean IS_SONYERICSSON = J2ME_PLATFORM.startsWith("sonyericsson");
//Method to check if the phone is using Nokia platform
public static final boolean IS_NOKIA = J2ME_PLATFORM.indexOf("nokia") != -1;
//Method to check if the phone is using Samsung platform
private static boolean isSamsung() {
try {
Class.forName("com.samsung.util.AudioClip");
return true;
} catch (Throwable t0) {
try{
Class.forName("com.samsung.util.Vibration");
return true;
}catch(Throwable t1){}
}
return false;
}
//Method to check if the phone is using LG platform
private static boolean isLG() {
try {
Class.forName("mmpp.media.MediaPlayer");
return true;
} catch (Throwable ex) {
try {
Class.forName("mmpp.phone.Phone");
return true;
} catch (Throwable ex1) {
try {
Class.forName("mmpp.lang.MathFP");
return true;
} catch (Throwable ex2) {
try {
Class.forName("mmpp.media.BackLight");
return true;
} catch (Throwable ex3) {
}
}
}
}
return false;
}
//Method to check if the phone is using Sony Ericsson platform
public static final boolean IS_SONYERICSSON = J2ME_PLATFORM.startsWith("sonyericsson");
//Method to check if the phone is using Nokia platform
public static final boolean IS_NOKIA = J2ME_PLATFORM.indexOf("nokia") != -1;
//Method to check if the phone is using Samsung platform
private static boolean isSamsung() {
try {
Class.forName("com.samsung.util.AudioClip");
return true;
} catch (Throwable t0) {
try{
Class.forName("com.samsung.util.Vibration");
return true;
}catch(Throwable t1){}
}
return false;
}
//Method to check if the phone is using LG platform
private static boolean isLG() {
try {
Class.forName("mmpp.media.MediaPlayer");
return true;
} catch (Throwable ex) {
try {
Class.forName("mmpp.phone.Phone");
return true;
} catch (Throwable ex1) {
try {
Class.forName("mmpp.lang.MathFP");
return true;
} catch (Throwable ex2) {
try {
Class.forName("mmpp.media.BackLight");
return true;
} catch (Throwable ex3) {
}
}
}
}
return false;
}
Thursday, 29 January 2009
Good programming practices
Hi programmers,
one really good practice is to have Code Review in which you ask someone else to review your code after you make changes to it.
The purpose of the code reviews is two-fold: to catch bugs early and to share the knowledge of the code base. Each time you review code you will learn something about the component you are reviewing as well as finding potential problems in the code. It is a very good way to learn for both the reviewer and the one whose code is reviewed.
ALso, it would be good to have the same coding convention like this one which is Sun's Java Coding Convention for any java code.
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
Good comments are also good to have. Look here: http://java.sun.com/j2se/javadoc/writingdoccomments/
one really good practice is to have Code Review in which you ask someone else to review your code after you make changes to it.
The purpose of the code reviews is two-fold: to catch bugs early and to share the knowledge of the code base. Each time you review code you will learn something about the component you are reviewing as well as finding potential problems in the code. It is a very good way to learn for both the reviewer and the one whose code is reviewed.
ALso, it would be good to have the same coding convention like this one which is Sun's Java Coding Convention for any java code.
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
Good comments are also good to have. Look here: http://java.sun.com/j2se/javadoc/writingdoccomments/
Subscribe to:
Posts (Atom)