Wednesday 26 August 2009

Java in Brew, iPod Touch/iPhone, Android

Javaground provides Xpress Suite which ports a singla Java source code into Brew, iPod Touch/iPhone, Android. Sounds amazing but I never got to try it. But you can try it and get more info from here:

http://www.javaground.com/

Sunday 23 August 2009

Application not compatible with phone error

Application not compatible with phone error happen because it is not set to the proper settings supported by the phone. It is a runtime error which means it would only happen if you run it on the phone itself.

To overcome this problem, you need to find out the CLC and MIDP version supported by the phone through Google etc. For example, if the phone supports CLDC 1.1 and MIDP 2.0, select that as the settings in your IDE(eg Netbeans,Sun Wireless Toolkit). Then, compile the code and run it in the phone. It should work now.

If not, another possibility is that you are using a JSR which the phone does not support. You can again google the JSR which your phone supports.

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");
  }
}

Friday 3 April 2009

Processes and Port Numbers

Need to find out what process is using a certain port? Use CurrPorts! Download it here. Download the whole zip file and extract it. There would be a number of programs inside. Open up CurrPorts and it would list all the processes using the respective port numbers.

To kill the process:
Now that you know what is the process, open Task Manager(CTRL + ALT + DEL) and search for the process. Select it and press End Process.

:)

I had to use this because there was a port being used and I could not do On-Device Debugging to my Sony Ericsson JP-8 phone. So this might help some of you people out there!

Thursday 2 April 2009

Minimizing MIDlets in Sony Ericsson

To check if a MIDlet is minimized, the Displayable.isShown() method can be used. It is of course preferable to check for a minimized MIDlet this way:

private boolean isOnBackGround(){
  boolean isOnBackGround =(BubbleTalk.getDisplay().getCurrent() == null ||
      !BubbleTalk.getDisplay().getCurrent().isShown());
  return isOnBackGround;
}


For more information regarding minimizing MIDlets in Sony Ericsson, click here.

Wednesday 1 April 2009

pauseApp() never called?

Just for your information...

"On Series 40 Development PLatform 1.0 phones, the pauseApp() method of your MIDlet will never be called, so you can detect whether your application is paused only by using the hideNotify() method of Canvas."

Taken from here

Check whether hideNotify() or isShown() methods will be called if u accept/reject the call. At times it may enter any of these methods.

On Device Debugging in Sony Ericsson

Sony Ericsson provides a very useful tool for us developers which is the On-Device Debugging. What makes it better is that they even have a wiki on this to document the steps on how to do an On-Device Debugging. Below is a useful video to help you out. It is however applicable for Java Platform 8 and above(probably).

For those below Java Platform 8, the steps are very similar to the one shown below. Only difference is that you need to find out the port number and select it in the Connection Proxy to connect the device to the PC.



Please take note that to enable Java Developer Mode(JP-8 Phones)
Click Right * Left Left * Left *

For phones of lower than JP-8, there is no need to enable the Java Developer Mode.

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 + "?");
}
}

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;
}

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/