⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msgmidlet.java

📁 这是一个很简单地利用WMA发送短消息的工程
💻 JAVA
字号:
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 ();
	      }
	    }
	  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -