如何实现打电话功能
文章信息
介绍
platformRequest()是MIDlet类的一个方法,通过这个方法可以访问设备的一些功能。它的定义如下:
如果URL的指定为tel:<number>(详细信息请参考RFC2806) 时,系统会将其转换成一个语音电话并且启动系统的电话程序去处理这个请求。参数中的电话号码会由系统的电话程序转化成DTMF信号。
源代码
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.midlet.MIDlet;
public class ExampleMIDlet extends MIDlet implements CommandListener {
private Command callCommand;
private Command exitCommand;
private Form mainForm;
// The phone number to call
private final String PHONE_NUMBER = "123456789";
/**
* Constructor. Constructs the object and initializes displayables.
*/
public ExampleMIDlet() {
mainForm = new Form("ExampleMIDlet");
callCommand = new Command("Call", Command.SCREEN, 0);
mainForm.addCommand(callCommand);
exitCommand = new Command("Exit", Command.EXIT, 0);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
}
/**
* Calls the specified number.
*/
private void call(String number) {
try {
platformRequest("tel:" + number);
} catch (ConnectionNotFoundException ex) {
// TODO: Exception handling
}
}
/**
* 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(PHONE_NUMBER);
} else if (command == exitCommand) {
// Exit the MIDlet
destroyApp(true);
notifyDestroyed();
}
}
}


(no comments yet)