SMS(短信)-J2ME的实现
来源:互联网 发布:潮汕牛肉丸淘宝哪家好 编辑:程序博客网 时间:2024/04/30 07:33
这是用J2ME来实现的发短信的一段代码
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.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
public class MsgMidlet extends MIDlet implements CommandListener {
private Display display;
private TextField textMesg;
private String strMesg;
private TextField dest;
private String strDest;
private Command doneCommand;
private Command send1Command;
private Command send2Command;
private String serverPort;
private boolean done;
private MessageConnection sconn;
public MsgMidlet () throws Exception {
display = Display.getDisplay(this);
doneCommand = new Command("DONE",
Command.SCREEN, 1);
send1Command = new Command("SEND1",
Command.SCREEN, 1);
send2Command = new Command("SEND2",
Command.SCREEN, 1);
serverPort = "5000";//getAppProperty("serverPort");
System.out.print("查到的Port=" + serverPort);
}
public void startApp() {
try {
displayBlankForm ();
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();
}
}
public void pauseApp() {
done = true;
try {
sconn.close();
} catch (Exception e) {
System.out.print("Error in pause");
e.printStackTrace();
}
}
public void destroyApp(boolean unconditional) {
done = true;
try {
sconn.close();
} catch (Exception e) {
System.out.print("Error in pause");
e.printStackTrace();
}
}
// There are some potentially blocking I/O
// operations in this callback function.
// In real world applications, you probably
// want to move them to a separate thread.
public void commandAction(Command command,
Displayable screen) {
if (command == doneCommand) {
destroyApp(false);
notifyDestroyed();
} else if (command == send1Command) {
try {
strDest = dest.getString();
strMesg = textMesg.getString();
Thread fetchThread=new Thread()
{
public void run(){
System.out.println("Try send1Command.....");
try
{
String addr = "sms://" + strDest;
System.out.println("发送地址为:" + addr);
MessageConnection conn =
(MessageConnection) Connector.open(addr);
TextMessage msg =
(TextMessage) conn.newMessage(
MessageConnection.TEXT_MESSAGE);
msg.setPayloadText( strMesg );
conn.send(msg);
conn.close();
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
};
fetchThread.start();
displayBlankForm ();
} catch (Exception e) {
System.out.println("Error in sending");
e.printStackTrace ();
}
} else if (command == send2Command) {
try {
strDest = dest.getString();
strMesg = textMesg.getString();
Thread Send2Thread = new Thread()
{
public void run(){
System.out.println("Try send2Command.....");
try
{
String addr = "sms://" + strDest;
System.out.println("发送地址为:" + addr);
TextMessage msg =
(TextMessage) sconn.newMessage(
MessageConnection.TEXT_MESSAGE);
msg.setAddress ( addr );
msg.setPayloadText( strMesg );
sconn.send(msg);
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
};
Send2Thread.start();
displayBlankForm ();
} catch (Exception e) {
System.out.println("Error in sending");
e.printStackTrace ();
}
}
}
private void displayBlankForm () throws Exception {
Form form = new Form ("WMATester");
textMesg = new TextField("Message", "",
100, TextField.ANY);
dest = new TextField("Phone No.", "",
20, TextField.ANY);
form.append( dest );
form.append( textMesg );
form.addCommand(doneCommand);
form.addCommand(send1Command);
form.addCommand(send2Command);
form.setCommandListener(
(CommandListener) this);
display.setCurrent(form);
}
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);
display.setCurrent( alert );
} else {
throw new Exception("Received is not a text mesg");
}
}
} catch (Exception e) {
System.out.println("Error in server receiving");
e.printStackTrace ();
}
}
}
}
- SMS(短信)-J2ME的实现
- Android下实现短信SMS的收发
- J2ME短信(WMA1.0-SMS)
- java实现sms短信发送
- 关于SMS短信滞后阅读的实现方案
- 通过广播简单实现sms短信的发送和接收
- 实现像handcent sms或者chomp sms那样的气泡短信样式
- 实现像handcent sms或者chomp sms那样的气泡短信样式
- Android——实现像handcent sms或者chomp sms那样的气泡短信样式
- j2me实现收发短信
- 短信SMS
- java 发送短信sms实现接口
- Java代码实现SMS短信发送功能
- Android UI开发第四篇——实现像handcent sms或者chomp sms那样的气泡短信样式
- Android UI开发第四篇——实现像handcent sms或者chomp sms那样的气泡短信样式
- Android UI开发第四篇——实现像handcent sms或者chomp sms那样的气泡短信样式
- Java通过SMS短信平台实现发短信功能
- Java通过SMS短信平台实现发短信功能
- Oracle难道真的很脆弱?
- AppFuseXML文件分析(一)
- 第2章 内存管理:2.10 页换入
- 忽然醒悟
- 王子与公主
- SMS(短信)-J2ME的实现
- 怎样从MIDI文件中提取出音符的绝对音高信息
- 第3章进程
- 流媒体的初研以前我也听说,好像kvm底层实现不太支持j2me来做streaming video/audio,但我不知道那人为什么这么说。
- 安装VMwaretools的方法
- 如何用seam实现文件上传
- C和VC中分配变量地址的问题
- 中间件的基本概念
- 关于向上转型和向下转型