smsform.java

来自「JavaME 手机短信息的收发」· Java 代码 · 共 96 行

JAVA
96
字号
/*
 * SMSForm1.java
 *
 * Created on 2008年6月13日, 上午10:53
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package sms;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

/**
 *
 * @author  Administrator
 * @version
 */
public class SMSForm extends Form implements CommandListener,Runnable{
    Command sendCommand=new Command("send",Command.OK,1);
    Command exitCommand=new Command("exit",Command.EXIT,2);
    TextField targetNumber;
    TextField smsContent;
    MyMidlet mm;
    String m_port;
    Display display;
    Alert a;
    
    public SMSForm(MyMidlet m,String arg, String port, Display d){
        super(arg);
        mm=m;
        display=d;
        m_port=port;
        targetNumber=new TextField("target number:","",11,TextField.NUMERIC);
        smsContent=new TextField("content:","",11,TextField.ANY);
        append(targetNumber);
        append(smsContent);
        this.addCommand(sendCommand);
        this.addCommand(exitCommand);
        this.setCommandListener(this);
    }
    
   /* public SMSForm(MyMidlet m,String arg, String port, Display d,String number){
        super(arg);
        mm=m;
        display=d;
        m_port=port;
        targetNumber=new TextField("target number:","",11,TextField.NUMERIC);
        smsContent=new TextField("content:","",11,TextField.ANY);
        targetNumber.setString(number);
        append(targetNumber);
        append(smsContent);
        this.addCommand(sendCommand);
        this.addCommand(exitCommand);
        this.setCommandListener(this);
    }*/
    
    public void commandAction(Command c, Displayable s){
        try{
            if(c==exitCommand){
                mm.exit();
            }else if(c==sendCommand){
                Thread h=new Thread(this);
                h.start();
                a=new Alert("status","sending...",null,AlertType.INFO);
                a.setTimeout(Alert.FOREVER);
                display.setCurrent(a);
            }
        }catch(Exception e){}
    }
    
    public void run(){
        String address="sms://"+targetNumber.getString()+":"+m_port;
        MessageConnection conn=null;
        try{
            conn=(MessageConnection)Connector.open(address);
            TextMessage txtm=(TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);//短信
            txtm.setAddress(address);//发送地址
            txtm.setPayloadText(smsContent.getString());//短信内容
            conn.send(txtm);
        }catch(Exception e){}
        
        if(conn!=null){
            try{
                conn.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        if(a!=null) a.setString("send successful!");
    }
}

⌨️ 快捷键说明

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