Making a phone call using TextField in Java ME
(Mtilli -) |
(Mtilli -) |
||
| Line 35: | Line 35: | ||
<code java> | <code java> | ||
| + | import javax.microedition.io.ConnectionNotFoundException; | ||
import javax.microedition.lcdui.Command; | import javax.microedition.lcdui.Command; | ||
import javax.microedition.lcdui.CommandListener; | import javax.microedition.lcdui.CommandListener; | ||
| Line 45: | Line 46: | ||
<code java> | <code java> | ||
| − | public class | + | public class MakingPhoneCall2 extends MIDlet implements CommandListener { |
| + | private TextField numberField; | ||
| + | private Command callCommand; | ||
private Command exitCommand; | private Command exitCommand; | ||
private Form mainForm; | private Form mainForm; | ||
| Line 52: | Line 55: | ||
* Constructor. Constructs the object and initializes displayables. | * Constructor. Constructs the object and initializes displayables. | ||
*/ | */ | ||
| − | public | + | public MakingPhoneCall2() { |
mainForm = new Form("ExampleMIDlet"); | mainForm = new Form("ExampleMIDlet"); | ||
| − | + | ||
// Create a phone number field, which allows a call to be made | // Create a phone number field, which allows a call to be made | ||
| − | + | numberField = new TextField("Phone number", null, 20, TextField.PHONENUMBER); | |
| − | + | mainForm.append(numberField); | |
| + | |||
| + | callCommand = new Command("Call", Command.SCREEN, 0); | ||
| + | mainForm.addCommand(callCommand); | ||
exitCommand = new Command("Exit", Command.EXIT, 0); | exitCommand = new Command("Exit", Command.EXIT, 0); | ||
mainForm.addCommand(exitCommand); | mainForm.addCommand(exitCommand); | ||
| + | |||
mainForm.setCommandListener(this); | mainForm.setCommandListener(this); | ||
} | } | ||
| Line 82: | Line 89: | ||
*/ | */ | ||
public void commandAction(Command command, Displayable displayable) { | public void commandAction(Command command, Displayable displayable) { | ||
| − | if (command == exitCommand) { | + | if (command == callCommand) { |
| + | call(); | ||
| + | } else if (command == exitCommand) { | ||
// Exit the MIDlet | // Exit the MIDlet | ||
destroyApp(true); | destroyApp(true); | ||
| Line 88: | Line 97: | ||
} | } | ||
} | } | ||
| + | /** | ||
| + | * Makes a call to number user typed in the text field. | ||
| + | */ | ||
| + | private void call() { | ||
| + | String number; | ||
| + | |||
| + | number = numberField.getString(); | ||
| + | try { | ||
| + | platformRequest("tel:" + number); | ||
| + | } catch (ConnectionNotFoundException ex) { | ||
| + | System.out.println(ex.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | } | ||
</code> | </code> | ||
Revision as of 12:00, 27 June 2012
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
Article Metadata
Code Example
Source file: Media:MakingPhoneCall2_Java.zip
Tested with
Devices(s): Nokia N95 8GB
Compatibility
Platform(s): S60 3rd Edition, FP1
Article
Keywords: javax.microedition.lcdui.TextField
Created: tapla
(07 May 2008)
Last edited: mtilli
(27 Jun 2012)
Contents |
Overview
This snippet demonstrates how to make a phone call by using a TextField. The MIDlet constructs a phone number field and displays it on the screen. The user may enter a phone number into it or obtain one from the Contacts list. After that, he or she can select Options > Call to call that number.
Source
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class MakingPhoneCall2 extends MIDlet implements CommandListener {
private TextField numberField;
private Command callCommand;
private Command exitCommand;
private Form mainForm;
/**
* Constructor. Constructs the object and initializes displayables.
*/
public MakingPhoneCall2() {
mainForm = new Form("ExampleMIDlet");
// Create a phone number field, which allows a call to be made
numberField = new TextField("Phone number", null, 20, TextField.PHONENUMBER);
mainForm.append(numberField);
callCommand = new Command("Call", Command.SCREEN, 0);
mainForm.addCommand(callCommand);
exitCommand = new Command("Exit", Command.EXIT, 0);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
}
/**
* Called when the MIDlet is started.
*/
public void startApp() {
Display.getDisplay(this).setCurrent(mainForm);
}
// Other inherited methods omitted for brevity
// ...
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param command the command that was invoked
* @param displayable the displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
if (command == callCommand) {
call();
} else if (command == exitCommand) {
// Exit the MIDlet
destroyApp(true);
notifyDestroyed();
}
}
/**
* Makes a call to number user typed in the text field.
*/
private void call() {
String number;
number = numberField.getString();
try {
platformRequest("tel:" + number);
} catch (ConnectionNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
}
Postconditions
A phone number field is displayed on the screen. The user can enter a phone number and select Options > Call to call that number.

