📄 smsmidlet.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */import javax.wireless.messaging.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.Alert;/** * 发送文本短信息的方法 */public class SMSMidlet extends MIDlet implements CommandListener{ Form MainFrm; TextField PhoneNumber; TextField Content; Command Send; Command Exit; boolean flag; Alert al; public SMSMidlet(){ flag=false; MainFrm=new Form("手机短信"); PhoneNumber=new TextField("手机号码","15071202480",11,TextField.ANY); Content=new TextField("短信内容",null,300,TextField.ANY); Send=new Command("发送",Command.OK,1); Exit=new Command("退出",Command.EXIT,1); MainFrm.append(PhoneNumber); MainFrm.append(Content); MainFrm.addCommand(Send); MainFrm.addCommand(Exit); MainFrm.setCommandListener(this); } /** * 给指定号码发送短信息 * @param content 短信息内容 * @param phoneNumber 手机号码 * @return 发送成功返回true,否则返回false */ public boolean send(String content, String phoneNumber) { //返回值 boolean result = true; try { //地址 String address = "sms://+" + phoneNumber; //建立连接 MessageConnection conn = (MessageConnection) Connector.open(address); //设置短信息类型为文本,短信息有文本和二进制两种类型 TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE); //设置信息内容 msg.setPayloadText(content); //发送 conn.send(msg); } catch (Exception e) { result = false; //未处理 } return result; } protected void startApp() throws MIDletStateChangeException { Display.getDisplay(this).setCurrent(MainFrm); } protected void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if(c==Send){ flag=send(Content.getString().trim(),PhoneNumber.getString().trim()); if(flag){ al=new Alert("提示信息","短信发送成功!",null,AlertType.ALARM); }else{ al=new Alert("提示信息","短信发送失败!",null,AlertType.WARNING); } al.setTimeout(Alert.FOREVER); Display.getDisplay(this).setCurrent(al); }else if(c==Exit){ destroyApp(true); notifyDestroyed(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -