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:-
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.
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.
hi. thanks a lots for this post. i want to ask about gps used in j2me. becoz i read that j2me dosent support gps perfectly.
ReplyDeletePlz tell me something about stop texting from gps when vechicle is in speed.
Hi Thanks for your comment.. and now regarding your query on GPS support using J2ME.. Frankly speaking I didn't have practical exposure on JSR 179 API.. so at present I am not able to comment anything... But ASAP I find some solution about your query, I'll post the solution. I am really sorry this time..
ReplyDeleteHi in j2me apllication i used the above code but i didnt get any call. my emulator number is 123456789. i used call.this.platformrequest("tel:+123456789"). where call is my MIDLet name.
ReplyDeleteHi Sridhar,
ReplyDeleteUse only call.platformRequest("tel:+" + numberString); Because platformRequest method is defined in MIDlet class. I used "this" because mine method is in same MIDlet class.
Hi! I need to get the call duration for outgoing calls in my j2me application. Since call logs cannot be accessed via j2me and just a rough estimate of the call duration is needed, I decided to get the time when platformrequest method was called. However, I cannot use pauseApp because it disconnects the socket connection. But if I did not invoke pauseApp, I need to track the time the user presses the end key. But the end key exits the application. So is there anything that I can do to solve my problem? Is there a way to wait for the end of call before resuming the application without using pauseapp? Please help me!
ReplyDeleteHi,
ReplyDeleteFirst of all apologizes for delay... As per your query, AFAIK we are not able to track the call duration using JavaME. But may be, you can initiate a timer thread and store/update that timer value in RMS. So if you press Call end button then also you have value in RMS. Call this timer function on the keyPressed for Call END key. Also at the Exit of your app. I am not sure that this will give you exact value but atleast you have approximate value. You can give a try for this workaround.
Best Regards,
Ashish
Hello Ashish,
ReplyDeleteMy query is regarding fetching contact.
When I tried to fetch contact name using Contact.NAME property, output i am getting last name of each contact from the phonebook. Contact.FORMATTED_NAME is not supported. Can you tell me how can i fetch first name of the contact?
for making a call do we need to connect a mobile to the system?cant we show calls in emulator itself?kindly someone help me out...
ReplyDelete@Silwar - Apologies for delay in reply, Please try to fetch desired info using Contact.FIRST_NAME & Contact.LAST_NAME.
ReplyDelete@Anonymous - To make a call you need not to connect your device to system. Either you can install your MIDlet on real device and test this functionality. To simulate call on emulator:- You can enter the number in text field & click on "Call" or if you are reading from contact list then after reading just click on the "Call". SDK will simulate the call functionality.
Hello Ashish...
ReplyDeletecan I stop a call using a timer, for example at 5 minutes
thanks
Hi Reader,
ReplyDeleteJ2ME has very limited set of Telephony APIs, so as per my knowledge this functionality is not feasible using J2ME APIs.
You can also check one of my other article for Block a call - http://goo.gl/pwSTK
Best Regards,
Ashish Sharma.
Hey Ashish,
ReplyDeletewonderful blog..keep up the good work..
i'm trying to develop a mobile app for nokia s40 phone, is it possible to make a call and know status of the call in terms or "call answered" "number busy" and "no answer"??
i want to know if the other person has cut the call or answered it or if he isnt at the desk at all..
if its not possible in s40 phones then is it possible in s60?? if so how?
thank you in advance for ur help..
Hi,
ReplyDeleteThanks for your words, surely I'll update few more articles in upcoming months.
As per your queries, we are surely able to initiate the call with in mobile application using J2ME & the article you have gone through. Now you want to know the status of call with in mobile application using J2ME, which is not possible using J2ME on both S40 & S60 devices. You have no access to the incoming or outgoing calls from MIDP 2 (except that you can place a phone call to a number with platformRequest()) due to the limited support of Telephony API on J2ME.
But on S60 devices you can use native Symbian APIs to build same functionality.
Best Regards,
Ashish Sharma.
Hi Ashish
ReplyDeleteI want to read the number by voice in j2me and make a call to this number also by voice.
please tell me something about working procedure for my project
To build this app, you should work on speech recognition functionality. So that your app can recognize the number and then initiate a call. The main bottle neck in this apps is speech recognition, because different user may have different accent. For speech recognition, please check @ http://goo.gl/6RtD0
ReplyDeletesudhir
ReplyDeletecan any body give code on mobile receiving a call without user initimation
sudhir9909@gmail.com
Hi Sudhir,
ReplyDeleteJ2ME has very limited support of Telephony API. So using J2ME you are not able to detect call receiving. With in J2ME App it's not possible.
Best Regards,
Ashish Sharma.
hi Ashish..
ReplyDeletethrough wifi i connect my phones and create wifi chat.. if it possible to make calls between those phones.. pls help me its urgent...
Yes you can make call using wifi connection to.. It should be the VOIP call.
ReplyDeleteI want to make a USSD call. How can i achieve it.
ReplyDeleteI successfully made a general voice call, but i want to make a USSD call now. Please help
You can't make USSD call using J2ME, because to initiate a call with in J2ME you need MSISDN in international number format.
ReplyDeleteBut you can use one workaround like this, you can initiate a normal call from J2ME App using proper format MSISDN and at the back-end you can re-direct/map that call to USSD call. To map/re-direct you need to collaborate with Telco people.
sudheer
ReplyDeletesir i want to run my mobile application background(i.e the phone user should not know the application is running) if the answer is their with please send to sudhir9909@gmail.com
sudhir
ReplyDeletewhat should i use in order to make an mobile application run background.
Hi Sudhir,
ReplyDeleteLike Android, we can't run J2ME app in background. But we can use Push Registry API to invoke app any time, if it receive any notification. AFAIK, other then that there is no other way.
--
Best Regards,
Ashish Sharma.