當前位置:名人名言大全網 - 短信平臺 - J2ME讀取手機短信

J2ME讀取手機短信

j2me通過WMA讀取短信

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

}

}

}

}

}