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

📄 mmsreceive.java

📁 Java ME手机应用开发大全一书的配套光盘上的源码
💻 JAVA
字号:

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;
import java.io.IOException;
public class MMSReceive extends MIDlet
    implements CommandListener, Runnable, MessageListener {

    private static final Command CMD_EXIT  =new Command("Exit", Command.EXIT, 2);
    private Form content;
    private Display display;
    //声明接收多媒体消息的线程
    private Thread thread;
    private String[] connections;
    //声明接收消息成功的标识位
    private boolean done;
    //声明监听多媒体消息的applicationID
    private String appID;
    //声明多媒体消息连接对象
    private MessageConnection mmsconn;
    //声明获得的多媒体消息的对象
    private Message msg;
    //声明多媒体消息发送方的地址
    private String senderAddress;
    private Alert sendingMessageAlert;
    private Displayable resumeScreen;
    //声明接收多媒体消息的标题
    private String subject;
    //声明接收多媒体消息的内容
    private String contents;

    public MMSReceive() {
		//初始化多媒体消息的applicationID
        appID = "MMSTest";

        display = Display.getDisplay(this);
        content = new Form("MMS Receive");
        content.addCommand(CMD_EXIT);
        content.setCommandListener(this);
        content.append("Receiving...");
        sendingMessageAlert = new Alert("MMS", null, null, AlertType.INFO);
        sendingMessageAlert.setTimeout(5000);
        sendingMessageAlert.setCommandListener(this);

        resumeScreen = content;
    }

    public void startApp() {
        //多媒体消息连接字符串
        String mmsConnection = "mms://:" + appID;
        //打开连接
        if (mmsconn == null) {
            try {
                mmsconn = (MessageConnection) Connector.open(mmsConnection);
                mmsconn.setMessageListener(this);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }

        //等待发送方发送多媒体消息
        connections = PushRegistry.listConnections(true);
        if (connections == null || connections.length == 0) {
            content.deleteAll();
            content.append("Waiting for MMS on applicationID " + appID + "...");
        }
        done = false;
        thread = new Thread(this);
        thread.start();

        display.setCurrent(resumeScreen);
    }

    /**
     * 当多媒体消息到来时,触发该监听方法
     */
    public void notifyIncomingMessage(MessageConnection conn) {
        if (thread == null && !done) {
            thread = new Thread(this);
            thread.start();
        }
    }

    //读取多媒体消息线程的执行方法
    public void run() {
        try {
            msg = mmsconn.receive();
            if (msg != null) {
                senderAddress = msg.getAddress();
                content.deleteAll();
                String titleStr = senderAddress.substring(6);
                int colonPos = titleStr.indexOf(":");
                if (colonPos != -1) {
                    titleStr = titleStr.substring(0, colonPos);
                }
                content.setTitle("From: " + titleStr);
                if (msg instanceof MultipartMessage) {
                    MultipartMessage mpm = (MultipartMessage)msg;
                    StringBuffer buff = new StringBuffer("Subject: ");
                    buff.append((subject = mpm.getSubject()));
                    buff.append("\nDate: ");
                    buff.append(mpm.getTimestamp().toString());
                    buff.append("\nContent:");
                    StringItem messageItem = new StringItem("Message",
							    buff.toString());
                    messageItem.setLayout(Item.LAYOUT_NEWLINE_AFTER);
                    content.append(messageItem);
                    MessagePart[] parts = mpm.getMessageParts();
                    if (parts != null) {
                        for (int i = 0; i < parts.length; i++) {
                            buff = new StringBuffer();
                            MessagePart mp = parts[i];
                            buff.append("Content-Type: ");
                            String mimeType = mp.getMIMEType();
                            buff.append(mimeType);
                            String contentLocation = mp.getContentLocation();
                            buff.append("\nContent:\n");
                            byte[] ba = mp.getContent();
                            if (mimeType.equals("image/png")) {
                                content.append(buff.toString());
                                Image img = Image.createImage(ba, 0, ba.length);
                                ImageItem ii = new ImageItem(contentLocation,
                                        img, Item.LAYOUT_NEWLINE_AFTER, 
                                        contentLocation);
                                content.append(ii);
                            } else {
                                buff.append(new String(ba));
                                StringItem si = new StringItem(
				        "Part", buff.toString());
                                si.setLayout(Item.LAYOUT_NEWLINE_AFTER);
                                content.append(si);
                            }
                        }
                    }
                }
                display.setCurrent(content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   
    public void pauseApp() {
        done = true;
        thread = null;
        resumeScreen = display.getCurrent();
    }

    public void destroyApp(boolean unconditional) {
        done = true;
        thread = null;
        if (mmsconn != null) {
            try {
                mmsconn.close();
            } catch (IOException e) {
                
            }
        }
    }
    public void commandAction(Command c, Displayable s) {
        try {
            if (c == CMD_EXIT || c == Alert.DISMISS_COMMAND) {
                destroyApp(false);
                notifyDestroyed();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

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