Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 06:59, 30 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Making a phone call using TextField in Java ME

From Wiki
Jump to: navigation, search

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. 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 MakingPhoneCallTF extends MIDlet implements CommandListener {
private TextField numberField;
private Command callCommand;
private Command exitCommand;
private Form mainForm;
 
/**
* Constructor. Constructs the object and initializes displayables.
*/

public MakingPhoneCallTF() {
mainForm = new Form("Making Phone Call");
 
// 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);
}
 
/**
* Called when MIDlet is paused.
*/

public void pauseApp() {
}
 
/**
* Called to signal the MIDlet to terminate.
*
* @param unconditional if true, then the MIDlet has to be unconditionally
* terminated and all resources has to be released.
*/

public void destroyApp(boolean unconditional) {
}
 
/**
* 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.

See also

This page was last modified on 30 July 2013, at 06:59.
132 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×