|
(转载) [yang1506 发表]
本人对j2me实现收发短信一直不懂,怎么也找不到这个wireless包,还请专业人士赐教,下面是在别人空间中转载的j2me发送短信的源代码!!!!!!!!!
在Nokia 6600上测试可发送短信^-^
public class sendThread extends Thread {
private String phone;
private String content;
public sendThread(String phone, String content) {
this.phone = phone;
this.content = content;
}
public void run() {
try {
String addr = "sms://" + phone;
//System.out.println("发送地址为:" + addr);
MessageConnection conn = (MessageConnection) Connector.open(addr);
TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(content);
conn.send(msg);
conn.close();
} catch (Exception e) {
//System.out.println("Error in sending");
e.printStackTrace();
}
}
}
第二种J2me发送短信的方法,Nokia 6600实机测试通过:-)
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
public class SendNoteMethod2 extends MIDlet implements CommandListener {
private Display dis;
private Command send;
private String serverPort;
private Form f;
private TextField tf1, tf2;
private boolean done;
private MessageConnection sconn;
public SendNoteMethod2() {
super();
dis = Display.getDisplay(this);
f = new Form("短信发送测试");
send = new Command("发送", Command.OK, 0);
tf1 = new TextField("电话:", null, 255, TextField.PHONENUMBER);
tf2 = new TextField("内容:", null, 255, TextField.ANY);
f.append(tf1);
f.append(tf2);
f.addCommand(send);
f.setCommandListener(this);
dis.setCurrent(f);
serverPort = "5000";
}
protected void startApp() throws MIDletStateChangeException {
try {
sconn = (MessageConnection) Connector.open("sms://:" + serverPort);
done = false;
new Thread(new SMSServer()).start();
} catch (Exception e) {
System.out.print("Error in start\n");
e.printStackTrace();
}
}
protected void pauseApp() {
done = true;
try {
sconn.close();
} catch (Exception e) {
System.out.print("Error in pause");
e.printStackTrace();
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
done = true;
try {
sconn.close();
} catch (Exception e) {
System.out.print("Error in pause");
e.printStackTrace();
}
}
public void commandAction(Command c, Displayable d) {
if (c == send) {
try {
Thread Send2Thread = new Thread() {
public void run() {
System.out.println("Try send2Command.....");
try {
String addr = "sms://" + tf1.getString();
System.out.println("发送地址为:" + addr);
TextMessage msg = (TextMessage) sconn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setAddress(addr);
msg.setPayloadText(tf2.getString());
sconn.send(msg);
} catch (Exception exc) {
exc.printStackTrace();
}
}
};
Send2Thread.start();
} catch (Exception e) {
System.out.println("Error in sending");
e.printStackTrace();
}
}
}
class SMSServer implements Runnable {
public void run() {
try {
while (!done) {
Message msg = sconn.receive();
if (msg instanceof TextMessage) {
TextMessage tmsg = (TextMessage) msg;
String msgText = tmsg.getPayloadText();
// Construct the return message
TextMessage rmsg = (TextMessage) sconn
.newMessage(MessageConnection.TEXT_MESSAGE);
rmsg.setAddress(tmsg.getAddress());
rmsg.setPayloadText("Message " + msgText
+ " is received");
sconn.send(rmsg);
Alert alert = new Alert("Received", msgText, null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
dis.setCurrent(alert);
} else {
throw new Exception("Received is not a text mesg");
}
}
} catch (Exception e) {
System.out.println("Error in server receiving");
e.printStackTrace();
}
}
}
}
|
|