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

📄 smssender.java

📁 改代码为基于j2me的多功能手机短机信收发
💻 JAVA
字号:

package sms;

import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;

import java.io.IOException;

/**
 * Prompts for text and sends it via an SMS MessageConnection
 */
 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;


    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);
    }

    public void promptAndSend(String destinationAddress)
    {
        this.destinationAddress = destinationAddress;
        display.setCurrent(messageBox);
    }


    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 + ":" + smsPort;

        MessageConnection smsconn = null;
        try {
            /** Open the message connection. */
            smsconn = (MessageConnection)Connector.open(address);


            TextMessage txtmessage = (TextMessage)smsconn.newMessage(
            MessageConnection.TEXT_MESSAGE);                     //不明白
            txtmessage.setAddress(address);
            txtmessage.setPayloadText(messageBox.getString());
            smsconn.send(txtmessage);
            if(true){System.out.println(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 + -