Wednesday, July 14, 2010

How to make a call in J2ME?

Hi Readers,

Today while working on my blog, I came to know that someone of you are searching for article on "How to make a call in J2ME?". So this article is especially for that user. In this article, I describe you about how to use your phone book & make a call on that number, also you can directly enter any international format number and make a call. So let's start:-

Here I'll show you two things - 1) Fetch contacts from your device phonebook, & 2) Make a Call

1) Fetch Contacts from your device phonebook:
We can do this activity in two ways - a) By using JSR 75 API, & b) By using TextField property constraint.

First method is bit complex and it require signing of application from device vendor, to avoid security prompt while accessing the device phonebook. So we'll skip this method and directly switch to second option. In this you need to use one of the textfield constraint "PHONEBOOK" to access the whole contact list using J2ME app. The code is as follows:-

TextField numberTxtField = new TextField("Please enter the number:", numberString, 12, TextField.PHONENUMBER);

When you use this code in your app, then on real device when the focus is on "numberTxtField" you will see the commands to "Search" on Nokia devices & "Lookup" on Sony devices. Commands may be differ from vendor to vendor but the functionality is same - to access the contact list of the device. Using this code we are able to see the contact list from device and even also we can enter any international format number in the textfield at runtime.

2) Make a Call:
Now the next step, how to make a call from J2ME application. It's very simple and you need to use platformRequest() method of MIDlet class and pass the number with "tel:" string; which make sure that method is used to make a call. To check more on the URI scheme, please visit - http://en.wikipedia.org/wiki/URI_scheme

The code will look like this:-

private boolean makeACall() {
        boolean isCalled = false;
        
//used to read the number from textfield
        numberString = numberTxtField.getString(); 
        try {
            if (numberString.indexOf("+") != -1) {
                isCalled = this.platformRequest("tel:" + numberString);
            } else {
                isCalled = this.platformRequest("tel:+" + numberString);
            }
            try {
                pauseApp();
                notifyPaused();
            } catch (Exception e) {
                //Nothing to do..
            }
        } catch (IllegalArgumentException e) {
            isCalled = false;
            //Handle the functionality for wrong format of number
        } catch (Exception ex) {
            isCalled = false;
        }
        return isCalled;
    }

Now you need to call this function at the "Call" command using command handler interface, and if you are using low level APIs then need to handle the key events. So using this method you can handle the call functionality in your J2ME application. The users who are looking for this functionality can let me know with their inputs.