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

📄 sendsmsmsg.java

📁 java me的一些源码,包括多媒体(声音、视频)
💻 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.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

public class SendSMSMsg extends MIDlet implements CommandListener {
	private Display display = null;
    
	/** 退出命令按钮 */
    private Command cmdExit = new Command("退出", Command.EXIT, 1);
    
    /** 发送消息的命令按钮 */
    private Command cmdSendMsg = new Command("发送", Command.SCREEN, 1);
    
    private Form form = new Form("发送文本信息");
    
    
    /** 用于输发送到的电话号码 */
    private TextField tfPhoneNumber = new TextField("请输入接收号码:",
    		"+5550000", 20,
            TextField.PHONENUMBER);
    /** 用于输发送端口 */
    private TextField tfPort = new TextField("请输入接收端口:",
    		"", 20,
            TextField.PHONENUMBER);
    /** 消息内容 */
    private TextField tfMsgText = new TextField("请输入消息内容:",
    		"", 255, 
    		TextField.ANY);
    
	public SendSMSMsg() {
		super();
		
		form.append(tfPhoneNumber);
		form.append(tfPort);
		form.append(tfMsgText);
               
        form.addCommand(cmdExit);
        form.addCommand(cmdSendMsg);
        form.setCommandListener(this);
	}

	protected void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        display.setCurrent(form);
	}

	protected void pauseApp() {
		// TODO 自动生成方法存根

	}

	protected void destroyApp(boolean arg0)
		throws MIDletStateChangeException {
		// TODO 自动生成方法存根

	}
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command c, Displayable s) {
        if (c == cmdExit) {
        	notifyDestroyed();
        } else if (c == cmdSendMsg) {
        	send();

        }
    }
    
    /**
     *  在独立的线程中发送消息
     */
    private void send() {
    	new Thread() {
    		public void run() {
    			//检查电话号码是否存在
    			String number = tfPhoneNumber.getString();
    			if (number.equals("")) {
    				Alert alert = new Alert("发送消息错误",
    						"请输入接收的电话号码", null,
    						AlertType.ERROR);
    				alert.setTimeout(2000);
    				display.setCurrent(alert, form);
    				AlertType.ERROR.playSound(display);
    				return;
    			} 

    			//地址
    			String address;
    			if (number.startsWith("+")) {
    				address = "sms://" + number;
    			} else {
    				address = "sms://+" + number;
    			}
    			  
    			//获得端口
    			String port = tfPort.getString();
	            if (!port.equals("")) {
	            	address+=":"+port;
	            }
	            System.out.println(address);
    			try {
    	            //建立连接
    	            MessageConnection mc = (MessageConnection)Connector.open(address);   
    	         
    	            //设置短信息类型为文本
    	            TextMessage msg = (TextMessage)mc.newMessage(
    	                    MessageConnection.TEXT_MESSAGE);
    	            //设置消息地址
    	            msg.setAddress(address);
    	            //设置信息内容
    	            msg.setPayloadText(tfMsgText.getString());
    	            //发送消息
    	            mc.send(msg);
                    mc.close();
                    mc = null;
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}.start();
    }
}

⌨️ 快捷键说明

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