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

📄 smssender.java

📁 看看如何用j2me 制作自己的短信制作和发送。 附带直接运行文件
💻 JAVA
字号:
/* * * Copyright © 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */package example.sms;import java.io.IOException;import java.lang.Integer;import javax.microedition.io.*;import javax.microedition.lcdui.*;import javax.wireless.messaging.*;/** * Prompts for text and sends it via an SMS MessageConnection */public class SMSSender implements CommandListener, Runnable {    /** user interface command for indicating Send request */    Command sendCommand = new Command("Send", Command.OK, 1);    /** user interface command for going back to the previous screen */    Command backCommand = new Command("Back", Command.BACK, 2);    /** Display to use. */    Display display;    /** The port on which we send SMS messages */    String smsPort;    /** The URL to send the message to */    String destinationAddress;    /** Area where the user enters a message to send */    TextBox messageBox;    /** Where to return if the user hits "Back" */    Displayable backScreen;    /** Displayed when a message is being sent */    Displayable sendingScreen;    /**     * Initialize the MIDlet with the current display object and     * graphical components.     */    public SMSSender(String smsPort, Display display, Displayable backScreen,        Displayable sendingScreen) {        this.smsPort = smsPort;        this.display = display;        this.destinationAddress = null;        this.backScreen = backScreen;        this.sendingScreen = sendingScreen;        messageBox = new TextBox("Enter Message", null, 65535, TextField.ANY);        messageBox.addCommand(backCommand);        messageBox.addCommand(sendCommand);        messageBox.setCommandListener(this);    }    /**     * Prompt for message and send it     */    public void promptAndSend(String destinationAddress) {        this.destinationAddress = destinationAddress;        display.setCurrent(messageBox);    }    /**     * Respond to commands, including exit     * @param c user interface command requested     * @param s screen object initiating the request     */    public void commandAction(Command c, Displayable s) {        try {            if (c == backCommand) {                display.setCurrent(backScreen);            } else if (c == sendCommand) {                display.setCurrent(sendingScreen);                new Thread(this).start();            }        } catch (Exception ex) {            ex.printStackTrace();        }    }    /**     * Send the message. Called on a separate thread so we don't have     * contention for the display     */    public void run() {        String address = destinationAddress;        String port;        // Figure out if we have a real port number        int portLocation = address.indexOf(":",5);        if (portLocation == -1 ) {            address = address + ":" + smsPort;        } else {            port = address.substring(portLocation+1);            address = address.substring(0,portLocation);            if (Integer.parseInt(port,10) != 0) {                address = address +":"+port;            } else                address = address +":"+smsPort;        }                            MessageConnection smsconn = null;        try {            /** Open the message connection. */            System.out.println("Will try to send:"+address+" message:"+messageBox.getString());            smsconn = (MessageConnection)Connector.open(address);                        TextMessage txtmessage =                (TextMessage)smsconn.newMessage(MessageConnection.TEXT_MESSAGE);            txtmessage.setAddress(address);            txtmessage.setPayloadText(messageBox.getString());            smsconn.send(txtmessage);        } catch (Throwable t) {            System.out.println("Send caught: ");            t.printStackTrace();        }        if (smsconn != null) {            try {                smsconn.close();            } catch (IOException ioe) {                System.out.println("Closing connection caught: ");                ioe.printStackTrace();            }        }    }}

⌨️ 快捷键说明

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