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

📄 wmabridgeapiexample.java

📁 在索爱K700C上可运行的J2ME发短信例程
💻 JAVA
字号:
/* * @(#)WMABridgeAPIExample.java	1.6 03/10/29 * * Copyright (c) 2000-2003 Sun Microsystems, Inc. All rights reserved.  * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms */import java.util.*;import java.io.*;import javax.swing.JOptionPane;import com.sun.kvem.midp.io.j2se.wma.client.WMAClient;import com.sun.kvem.midp.io.j2se.wma.Message;/* * This class shows an example usage of the Wireless Toolkit WMA Bridge API * for connecting J2SE clients to the toolkit's messaging environment. * * Use the build and run scripts in the same directory to build and run the * example. * * See the WMA Bridge API documentation included with the Toolkit for more * information on using the API. */public class WMABridgeAPIExample implements WMAClient.MessageListener {        public static int DEFAULT_SMS_PORT = 50000;    public static int DEFAULT_CBS_MESS_ID = 50001;        private WMAClient wtkClient;    private BufferedReader inputReader;    private int smsPort = DEFAULT_SMS_PORT;    private int cbsMessID = DEFAULT_CBS_MESS_ID;        public WMABridgeAPIExample() throws Exception {        // null argument for the phone number - we'll be assigned one.        wtkClient = new WMAClient(null, WMAClient.SEND_AND_RECEIVE);                wtkClient.connect();        wtkClient.setMessageListener(this);                // connect the standard input to our reader        inputReader = new BufferedReader(new InputStreamReader(System.in));    }    /**     * shows help on using this utility     */    private void displayHelp() {        System.out.println("Send and receive SMS and CBS messages " +            "to and from Wireless Toolkit emulators.\n" +            "Commands:\n" +            " sms <phoneNumber> <message> : send sms text message to phoneNumber\n" +            " cbs <message>               : send cbs text message broadcast\n" +            " getPort                     : display the port number to which sms is sent\n"+            " getMessageID                : display the message ID on which cbs is sent\n"+            " setPort <port>              : set the port to which sms is sent\n" +            " setMessageID <messID>       : set the message ID on which cbs is sent\n" +            " help                        : this help screen\n" +             " quit                        : exit the program");    }        /**     * show the sms port     */    private void displayPort() {        System.out.println("Using port " + smsPort + " for sms messages.");    }    /**     * show the cbs message ID     */    private void displayMessageID() {        System.out.println("Using message ID " + cbsMessID + " for cbs messages.");    }        /**     * handle the setPort command     */    private void handleSetPort(String argString) {        if (argString.length() > 0) {            try {                int port = Integer.parseInt(argString);                if (port >= 0 && port <= 65535) {                    smsPort = port;                    displayPort();                } else {                    System.out.println("Port must be in the range 0 to 65535.");                }               } catch (NumberFormatException nfe) {                System.out.println("Invalid number format");            }        } else {            System.out.println("setPort requires a port number.");        }    }        /**     * handle the setMessageID command     */    private void handleSetMessageID(String argString) {        if (argString.length() > 0) {            try {                int messID = Integer.parseInt(argString);                if (messID >= 0 && messID <= 65535) {                    cbsMessID = messID;                    displayMessageID();                } else {                    System.out.println("Message ID must be in the range 0 to 65535.");                }            } catch (NumberFormatException nfe) {                System.out.println("Invalid number format");            }        } else {            System.out.println("setMessageID requires a message ID number.");        }    }        /**      * handle the sms command     */    private void handleSMS(String argString) {        int spacePos = argString.indexOf(' ');        String toPhoneNumber;        String messageStr;        if (spacePos > 0) {            toPhoneNumber = argString.substring(0, spacePos);            messageStr = argString.substring(spacePos+1).trim();        } else {            System.out.println("sms requires a phone number and a message.");            return;        }        Message message = new Message(messageStr);        message.setToAddress("sms://" + toPhoneNumber + ":" + smsPort);        message.setFromAddress("sms://" + wtkClient.getPhoneNumber());                try {            wtkClient.send(message);            System.out.println("Sent sms.");        } catch (Exception e) {            System.err.println("Caught exception sending sms: ");            e.printStackTrace();        }    }        /**      * handle the cbs command     */    private void handleCBS(String argString) {        if (argString.length() > 0) {                        Message message = new Message(argString);            message.setToAddress("cbs://:" + cbsMessID);            try {                wtkClient.send(message);                System.out.println("Sent cbs.");            } catch (Exception e) {                System.err.println("Caught exception sending cbs: ");                e.printStackTrace();            }        } else {            System.out.println("cbs requires a message.");        }    }            /**     * get a command and perform it     * returns false when it's time to quit     */    private boolean processNextCommand() throws IOException {        System.out.print("Command: ");        String commandLine = inputReader.readLine();        if (commandLine==null || commandLine.length()==0) {            return true;        }        commandLine = commandLine.trim();        int spacePos = commandLine.indexOf(' ');        String firstWord;        String args;        if (spacePos > 0) { // more than 1 word            firstWord = commandLine.substring(0, spacePos);            args = commandLine.substring(spacePos+1).trim();        } else { // only 1 word            firstWord = commandLine;            args = "";        }        // perform the appropriate action for this command        if (firstWord.equalsIgnoreCase("getport")) {            displayPort();        } else if (firstWord.equalsIgnoreCase("getmessageid")) {            displayMessageID();        } else if (firstWord.equalsIgnoreCase("help")) {            displayHelp();        } else if (firstWord.equalsIgnoreCase("quit") ||             firstWord.equalsIgnoreCase("exit")) {            return false;        } else if (firstWord.equalsIgnoreCase("setport")) {            handleSetPort(args);        } else if (firstWord.equalsIgnoreCase("setmessageid")) {            handleSetMessageID(args);        } else if (firstWord.equalsIgnoreCase("sms")) {            handleSMS(args);        } else if (firstWord.equalsIgnoreCase("cbs")) {            handleCBS(args);        }        return true;       }        /**     * the main loop to drive the program     */    public void driverLoop() throws Exception {        System.out.println("Running WMABridgeAPIExample. Phone number is " +            wtkClient.getPhoneNumber());        displayHelp();        displayPort();        displayMessageID();        while (true) {            try {                if (processNextCommand() == false) {                    break;                }            } catch (Exception e) {                System.err.println("Exception caught: " + e);                e.printStackTrace();            }        }        wtkClient.setMessageListener(null);        wtkClient.unregisterFromServer();    }        /**     * callback to report a received message     * specified by the WMAClient.MessageListener interface     */    public void notifyIncomingMessage(WMAClient client) {        try {            Message mess = wtkClient.receive();            String title;            if (mess.isSMS()) {                title = "SMS from " + mess.getFromAddress() + " on port " +                     mess.getToPort();            } else {                title = "CBS with message ID " +                    mess.getToPort();            }            // notify the user of the new message            JOptionPane.showMessageDialog(null,                                   mess.toString(),                                   title,                                   JOptionPane.PLAIN_MESSAGE);        } catch (IOException ioe) {            System.err.println("Caught executing:");            ioe.printStackTrace();        }    }        public static void main(String[] args) {        try {            WMABridgeAPIExample example = new WMABridgeAPIExample();            example.driverLoop();        } catch (Exception e) {            System.err.println("WMABridgeAPIExample caught:");            e.printStackTrace();        }        System.exit(0);    }}

⌨️ 快捷键说明

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