SMS(短信)-J2ME的实现

来源:互联网 发布:潮汕牛肉丸淘宝哪家好 编辑:程序博客网 时间:2024/04/30 07:33

这是用J2ME来实现的发短信的一段代码

import javax.microedition.io.Connector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
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;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

public class MsgMidlet extends MIDlet  implements CommandListener {
   private Display display;
   private TextField textMesg;
   private String strMesg;
   private TextField dest;
   private String strDest;
   private Command doneCommand;
   private Command send1Command;
   private Command send2Command;
   private String serverPort;
   private boolean done;
   private MessageConnection sconn;

   public MsgMidlet () throws Exception {
     display = Display.getDisplay(this);
 
     doneCommand = new Command("DONE",
                               Command.SCREEN, 1);
     send1Command  = new Command("SEND1",
                               Command.SCREEN, 1);
     send2Command  = new Command("SEND2",
                               Command.SCREEN, 1);
 
     serverPort = "5000";//getAppProperty("serverPort");
  System.out.print("查到的Port=" + serverPort);
   }

   public void startApp() {
     try {
       displayBlankForm ();
       sconn = (MessageConnection)
         Connector.open("sms://:" + serverPort);
       done = false;
       new Thread(new SMSServer()).start();
     } catch (Exception e) {
       System.out.print("Error in start/n");
       e.printStackTrace();
     }
   }
 
   public void pauseApp() {
     done = true;
     try {
       sconn.close();
     } catch (Exception e) {
       System.out.print("Error in pause");
       e.printStackTrace();
     }
   }
 
   public void destroyApp(boolean unconditional) {
     done = true;
     try {
       sconn.close();
     } catch (Exception e) {
       System.out.print("Error in pause");
       e.printStackTrace();
     }
   }

   // There are some potentially blocking I/O
   // operations in this callback function.
   // In real world applications, you probably
   // want to move them to a separate thread.

   public void commandAction(Command command,
                             Displayable screen) {
     if (command == doneCommand) {
       destroyApp(false);
       notifyDestroyed();
     } else if (command == send1Command) {
       try {
    
     strDest = dest.getString();
     strMesg = textMesg.getString();
    
     Thread fetchThread=new Thread()
             {
     public void run(){ 
      System.out.println("Try send1Command.....");
      
      try
      {
             String addr = "sms://" + strDest;
       System.out.println("发送地址为:" + addr);
             MessageConnection conn =
               (MessageConnection) Connector.open(addr);
             TextMessage msg =
                (TextMessage) conn.newMessage(
                MessageConnection.TEXT_MESSAGE);
             msg.setPayloadText( strMesg );
             conn.send(msg);
             conn.close();
      }
      catch(Exception exc)
      {
       exc.printStackTrace();
      }
     }
             };
    fetchThread.start();
    
         displayBlankForm ();
       } catch (Exception e) {  
         System.out.println("Error in sending");
         e.printStackTrace ();
       }
     } else if (command == send2Command) {
       try {
    
     strDest = dest.getString();
     strMesg = textMesg.getString();
    
     Thread Send2Thread = new Thread()
             {
     public void run(){ 
      System.out.println("Try send2Command.....");
      
      try
      {
             String addr = "sms://" + strDest;
       System.out.println("发送地址为:" + addr);
             TextMessage msg =
                (TextMessage) sconn.newMessage(
                MessageConnection.TEXT_MESSAGE);
             msg.setAddress ( addr );
             msg.setPayloadText( strMesg );
             sconn.send(msg);
      }
      catch(Exception exc)
      {
       exc.printStackTrace();
      }
     }
             };
    Send2Thread.start();
    
         displayBlankForm ();
       } catch (Exception e) {
         System.out.println("Error in sending");
         e.printStackTrace ();
       }
     }
   }

   private void displayBlankForm () throws Exception {
     Form form = new Form ("WMATester");
     textMesg = new TextField("Message", "",
                            100, TextField.ANY);
     dest = new TextField("Phone No.", "",
                        20, TextField.ANY);
     form.append( dest );
     form.append( textMesg );
     form.addCommand(doneCommand);
     form.addCommand(send1Command);
     form.addCommand(send2Command);
     form.setCommandListener(
                    (CommandListener) this);
     display.setCurrent(form);
   }

   class SMSServer implements Runnable {
     public void run () {
       try {
         while (!done) {
           Message msg = sconn.receive();
           if (msg instanceof TextMessage) {
             TextMessage tmsg = (TextMessage) msg;
             String msgText = tmsg.getPayloadText();

             // Construct the return message
             TextMessage rmsg =
                (TextMessage) sconn.newMessage(
                MessageConnection.TEXT_MESSAGE);
             rmsg.setAddress ( tmsg.getAddress() );
             rmsg.setPayloadText( "Message " +
                                  msgText +
                                  " is received" );
             sconn.send(rmsg);

             Alert alert = new Alert ("Received", msgText,
                                null, AlertType.ERROR);
             alert.setTimeout(Alert.FOREVER);
             display.setCurrent( alert );
           } else {
             throw new Exception("Received is not a text mesg");
           }
         }
       } catch (Exception e) {
         System.out.println("Error in server receiving");
         e.printStackTrace ();
       }
     }
   }
}

 
原创粉丝点击