J2ME 发短信 实例

package com.wanchong.duanxin;

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 MainMidlet 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 MainMidlet()
{
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();
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值