发短消息的程序

来源:互联网 发布:淘宝snp旗舰店是正品吗 编辑:程序博客网 时间:2024/04/30 11:54

下面的程序是用来发短信的,本人用它给自己的手机发了三条,结果只是发出去,没有收到,倒是扣了2.4元,平均每条短信0.8元。

目前还不知道是什么原因,自己再研究研究吧,你如果知道一定要告诉我哦。

 

public class MailSender extends MIDlet implements CommandListener{

    private Display display;
    private Form form;
    private TextField messageTo;
    private TextField content;
    private Command exit;
    private Command send;
    private Alert alert;

    public MailSender(){
        display = Display.getDisplay(this);
        form = new Form("My Message Sender");
        messageTo = new TextField("PhoneNumber:","",1513,TextField.NUMERIC);
        content = new TextField("Content:","",50,TextField.ANY);
        exit = new Command("Exit",Command.EXIT,1);
        send = new Command("Send",Command.OK,2);
        form.append(messageTo);
        form.append(content);
        form.addCommand(exit);
        form.addCommand(send);
        form.setCommandListener(this);
    }
    public void startApp() {
        display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
       if(c == send){
           boolean result = SMSUtil.sendMessage(messageTo.getString(),content.getString());
           if(result){
               alert = new Alert("Notice","Send OK!",null, AlertType.INFO);
           }
           else{
               alert = new Alert("Notice","Send Failed!",null, AlertType.INFO);
           }
           display.setCurrent(alert);
       }
       else if(c == exit){
           destroyApp(true);
           notifyDestroyed();
       }
    }
}

 

public class SMSUtil {
     /**
      * 给指定号码发送短信息
      * @param phoneNumber 手机号码
      * @param content 短信息内容
      * @return 发送成功返回true,否则返回false
      */
     public static boolean sendMessage(String phoneNumber,String content){
         //返回值
         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;
       }    
}

原创粉丝点击