给手机发送短信息主要有2种方式,一是通过移动通信网关实现,这个是大多数网站注册使用的方式,示例代码如下: // Sample message listener program.
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;
boolean done;
Reader reader;
// Initial tests setup and execution.
public void startApp() {
try {
// Get our receiving port connection.
messconn = (MessageConnection)
Connector.open(“sms://:6222”);
// Register a listener for inbound messages.
messconn.setMessageListener(this);
// Start a message-reading thread.
done = false;
reader = new Reader();
new Thread(reader).start();
} catch (IOException e) {
// Handle startup errors
}
}
// Asynchronous callback for inbound message.
public void notifyIncomingMessage(MessageConnection conn) {
if (conn == messconn) {
reader.handleMessage();
}
}
// 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 {
private int pendingMessages = 0;
// The run method performs the actual message reading.
public void run() {
while (!done) {
synchronized(this) {
if (pendingMessages == 0) {
try {
wait();
} catch (Exception e) {
// Handle interruption
}
},二是使用串口来发送信息,这个对你估计不实用,需要modem来连接手机
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;
boolean done;
Reader reader;
// Initial tests setup and execution.
public void startApp() {
try {
// Get our receiving port connection.
messconn = (MessageConnection)
Connector.open(“sms://:6222”);
// Register a listener for inbound messages.
messconn.setMessageListener(this);
// Start a message-reading thread.
done = false;
reader = new Reader();
new Thread(reader).start();
} catch (IOException e) {
// Handle startup errors
}
}
// Asynchronous callback for inbound message.
public void notifyIncomingMessage(MessageConnection conn) {
if (conn == messconn) {
reader.handleMessage();
}
}
// 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 {
private int pendingMessages = 0;
// The run method performs the actual message reading.
public void run() {
while (!done) {
synchronized(this) {
if (pendingMessages == 0) {
try {
wait();
} catch (Exception e) {
// Handle interruption
}
},二是使用串口来发送信息,这个对你估计不实用,需要modem来连接手机
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货