首页 arrow J2ME开发 arrow 程序设计 arrow j2me通过WMA读取短信
j2me通过WMA读取短信
Author Author: Wupei | Date Date: 2008-11-23 | View Count View: 2791 | Section & Category J2ME开发 - 程序设计 | Digg Digg: 1

import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
 
public class Example extends MIDlet implements MessageListener {
    MessageConnection messconn;
    int total = 0;
    boolean done;
 
 
    // Initial tests setup and execution.
 
    public void startApp() {
        try {
            // Get our receiving port conection.
            messconn = (MessageConnection)
                       Connector.open("sms://:6222");
 
            // Register a listener for inbound messages.
            messconn.setMessageListener(this);
 
            // Start a message-reading thread.
            done = false;
            new Thread(new Reader()).start();
 
        } catch (IOException e) {
            // Handle startup errors
        }
    }
 
 
    // Asynchronous callback for inbound message.
 
    public void notifyIncomingMessage(MessageConnection conn) {
        if (conn == messconn) {
            total++;
        }
    }
 
 
    // Required MIDlet method - release the connection and
    // signal the reader thread to terminate.
 
    public void pauseApp() {
        done = true;
        try {
            messconn.close();
        } catch (IOException e) {
            // Handle errors
        }
    }
 
 
    // Required MIDlet method - shutdown.
    // @param unconditional forced shutdown flag
 
    public void destroyApp(boolean unconditional) {
        done = true;
        try {
            messconn.setMessageListener(null);
            messconn.close();
        } catch (IOException e) {
            // Handle shutdown errors.
        }
    }
 
 
    // Isolate blocking I/O on a separate thread, so callback
    // can return immediately.
 
    class Reader implements Runnable {
 
        // The run method performs the actual message reading.
 
        public void run() {
            while (!done) {
                try {
                    Message mess = messconn.receive();
                } catch (IOException ioe) {
                    // Handle reading errors
                }
            }
        }
    }
}


更多阅读:

 
   评论 (1)
评论的 RSS
1
by: lf (游客) 2010-08-03 20:58
看看
回复该评论

我要发表评论

登录菜单

最新文章

本月热门

订阅本站

RSS 0.91 RSS 1.0 RSS 2.0 ATOM 0.3 OPML